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

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

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

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

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

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

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