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

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

j = A("j")
h = B("h")
i = A("i")
b = B("b")
d = B("d")
c = B("c")

j.o = i
j.q = i
h.r = c
i.o = j
i.q = j
b.r = h
d.r = b
c.r = b
assert b.r.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.o: A = None
        self.q: A = None
        self.txt = txt

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

j = A("j")
h = B("h")
i = A("i")
b = B("b")
d = B("d")
c = B("c")

j.o = i
j.q = i
h.r = c
i.o = j
i.q = j
b.r = h
d.r = b
c.r = b
assert b.r.r.r.r.txt == "h"