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

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

f = A("f")
j = B("j")
k = A("k")
h = A("h")
d = B("d")
c = A("c")
b = A("b")

f.q = j
f.o = h
j.p = c
k.q = d
k.o = b
h.q = d
h.o = b
d.p = f
c.q = j
c.o = b
b.q = j
b.o = f
assert d.p.q.p.q.p.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.o: A = None
        self.txt = txt

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

f = A("f")
j = B("j")
k = A("k")
h = A("h")
d = B("d")
c = A("c")
b = A("b")

f.q = j
f.o = h
j.p = c
k.q = d
k.o = b
h.q = d
h.o = b
d.p = f
c.q = j
c.o = b
b.q = j
b.o = f
assert d.p.q.p.q.p.txt == "c"