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

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

h = A("h")
a = B("a")
i = B("i")
k = A("k")
m = A("m")
d = A("d")
c = B("c")
j = A("j")
f = B("f")
n = A("n")

h.q = a
a.w = i
a.p = f
i.w = a
i.p = c
k.q = f
m.q = c
d.q = a
c.w = a
c.p = i
j.q = c
f.w = i
f.p = a
n.q = c
assert f.w.w.p.p.w.p.p.w.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.txt = txt

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

h = A("h")
a = B("a")
i = B("i")
k = A("k")
m = A("m")
d = A("d")
c = B("c")
j = A("j")
f = B("f")
n = A("n")

h.q = a
a.w = i
a.p = f
i.w = a
i.p = c
k.q = f
m.q = c
d.q = a
c.w = a
c.p = i
j.q = c
f.w = i
f.p = a
n.q = c
assert f.w.w.p.p.w.p.p.w.txt == "a"