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

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

n = A("n")
m = B("m")
f = A("f")
k = A("k")
b = B("b")
d = A("d")
h = A("h")
e = A("e")
g = B("g")

n.q = d
m.o = b
m.y = n
f.q = e
k.q = d
b.o = g
b.y = n
d.q = h
h.q = d
e.q = k
g.o = m
g.y = f
assert b.y.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.txt = txt

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

n = A("n")
m = B("m")
f = A("f")
k = A("k")
b = B("b")
d = A("d")
h = A("h")
e = A("e")
g = B("g")

n.q = d
m.o = b
m.y = n
f.q = e
k.q = d
b.o = g
b.y = n
d.q = h
h.q = d
e.q = k
g.o = m
g.y = f
assert b.y.q.q.q.txt == "d"