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

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

a = A("a")
k = B("k")
d = B("d")
m = B("m")
i = A("i")
f = B("f")
b = A("b")
h = A("h")

a.s = h
a.q = k
k.p = f
k.t = d
d.p = f
d.t = m
m.p = k
m.t = d
i.s = a
i.q = k
f.p = d
f.t = m
b.s = h
b.q = k
h.s = b
h.q = k
assert m.p.p.p.p.t.t.txt == "
class A:
    def __init__(self, txt: str):
        self.s: A = None
        self.q: B = None
        self.txt = txt

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

a = A("a")
k = B("k")
d = B("d")
m = B("m")
i = A("i")
f = B("f")
b = A("b")
h = A("h")

a.s = h
a.q = k
k.p = f
k.t = d
d.p = f
d.t = m
m.p = k
m.t = d
i.s = a
i.q = k
f.p = d
f.t = m
b.s = h
b.q = k
h.s = b
h.q = k
assert m.p.p.p.p.t.t.txt == "d"