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

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

b = A("b")
d = B("d")
h = A("h")

b.q = h
d.t = h
h.q = b
assert h.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.t: A = None
        self.txt = txt

b = A("b")
d = B("d")
h = A("h")

b.q = h
d.t = h
h.q = b
assert h.q.q.q.txt == "b"