class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.q: A = None
        self.txt = txt

j = A("j")
i = B("i")
m = A("m")

j.q = m
i.q = j
m.q = j
assert i.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.q: A = None
        self.txt = txt

j = A("j")
i = B("i")
m = A("m")

j.q = m
i.q = j
m.q = j
assert i.q.q.q.txt == "j"