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

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

d = A("d")
i = B("i")
f = B("f")
b = B("b")
c = A("c")
g = A("g")
h = A("h")
m = A("m")

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

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

d = A("d")
i = B("i")
f = B("f")
b = B("b")
c = A("c")
g = A("g")
h = A("h")
m = A("m")

d.r = h
i.q = b
i.o = c
f.q = i
f.o = c
b.q = i
b.o = d
c.r = d
g.r = c
h.r = g
m.r = d
assert b.o.r.r.r.r.r.r.r.txt == "c"