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

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

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

m.q = b
b.q = m
d.q = b
assert d.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.txt = txt

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

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

m.q = b
b.q = m
d.q = b
assert d.q.q.q.txt == "b"