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

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

b = A("b")
d = B("d")
j = B("j")
i = A("i")
a = B("a")
f = B("f")
g = B("g")
h = B("h")
e = A("e")

b.q = f
d.s = j
j.s = f
i.q = a
a.s = h
f.s = h
g.s = d
h.s = j
e.q = d
assert j.s.s.s.s.s.s.s.s.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.txt = txt

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

b = A("b")
d = B("d")
j = B("j")
i = A("i")
a = B("a")
f = B("f")
g = B("g")
h = B("h")
e = A("e")

b.q = f
d.s = j
j.s = f
i.q = a
a.s = h
f.s = h
g.s = d
h.s = j
e.q = d
assert j.s.s.s.s.s.s.s.s.txt == "h"