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

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

d = A("d")
g = B("g")
e = A("e")
k = A("k")
a = B("a")
c = B("c")

d.o = k
g.o = d
g.q = k
e.o = d
k.o = e
a.o = k
a.q = k
c.o = d
c.q = d
assert k.o.o.o.o.o.txt == "
class A:
    def __init__(self, txt: str):
        self.o: A = None
        self.txt = txt

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

d = A("d")
g = B("g")
e = A("e")
k = A("k")
a = B("a")
c = B("c")

d.o = k
g.o = d
g.q = k
e.o = d
k.o = e
a.o = k
a.q = k
c.o = d
c.q = d
assert k.o.o.o.o.o.txt == "d"