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

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

a = A("a")
d = B("d")
i = B("i")
e = A("e")
f = A("f")
j = A("j")

a.q = e
a.p = d
d.o = j
i.o = a
e.q = f
e.p = i
f.q = j
f.p = i
j.q = e
j.p = d
assert j.p.o.q.p.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.p: B = None
        self.txt = txt

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

a = A("a")
d = B("d")
i = B("i")
e = A("e")
f = A("f")
j = A("j")

a.q = e
a.p = d
d.o = j
i.o = a
e.q = f
e.p = i
f.q = j
f.p = i
j.q = e
j.p = d
assert j.p.o.q.p.txt == "i"