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

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

i = A("i")
f = B("f")
d = B("d")
c = B("c")
m = B("m")
h = B("h")

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

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

i = A("i")
f = B("f")
d = B("d")
c = B("c")
m = B("m")
h = B("h")

i.u = f
i.p = m
f.q = m
d.q = f
c.q = f
m.q = h
h.q = c
assert m.q.q.q.q.q.txt == "h"