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

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

a = A("a")
m = B("m")
l = B("l")
e = B("e")
d = A("d")

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

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

a = A("a")
m = B("m")
l = B("l")
e = B("e")
d = A("d")

a.t = m
m.q = d
m.t = a
l.q = d
l.t = d
e.q = a
e.t = d
d.t = l
assert l.q.t.q.txt == "d"