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.o: B = None
        self.txt = txt

a = A("a")
l = B("l")
f = B("f")
b = A("b")
g = A("g")

a.q = g
l.q = a
l.o = f
f.q = g
f.o = l
b.q = g
g.q = b
assert b.q.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.o: B = None
        self.txt = txt

a = A("a")
l = B("l")
f = B("f")
b = A("b")
g = A("g")

a.q = g
l.q = a
l.o = f
f.q = g
f.o = l
b.q = g
g.q = b
assert b.q.q.q.q.txt == "b"