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

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

l = A("l")
g = B("g")
d = B("d")
c = B("c")

l.r = g
l.q = c
g.o = c
g.q = l
d.o = c
d.q = l
c.o = d
c.q = l
assert g.o.o.o.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.q: B = None
        self.txt = txt

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

l = A("l")
g = B("g")
d = B("d")
c = B("c")

l.r = g
l.q = c
g.o = c
g.q = l
d.o = c
d.q = l
c.o = d
c.q = l
assert g.o.o.o.txt == "c"