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

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

j = A("j")
d = B("d")
m = A("m")
n = A("n")
b = B("b")

j.p = n
j.q = m
d.o = j
m.p = j
m.q = n
n.p = m
n.q = j
b.o = n
assert b.o.p.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: A = None
        self.q: A = None
        self.txt = txt

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

j = A("j")
d = B("d")
m = A("m")
n = A("n")
b = B("b")

j.p = n
j.q = m
d.o = j
m.p = j
m.q = n
n.p = m
n.q = j
b.o = n
assert b.o.p.p.p.txt == "n"