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

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

d = A("d")
a = B("a")
e = B("e")
b = A("b")

d.r = a
d.q = b
a.w = b
a.t = d
e.w = b
e.t = d
b.r = e
b.q = d
assert a.t.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.q: A = None
        self.txt = txt

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

d = A("d")
a = B("a")
e = B("e")
b = A("b")

d.r = a
d.q = b
a.w = b
a.t = d
e.w = b
e.t = d
b.r = e
b.q = d
assert a.t.q.q.q.txt == "b"