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

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

n = A("n")
m = B("m")
h = B("h")
f = B("f")
e = B("e")
i = A("i")
a = B("a")
d = B("d")

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

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

n = A("n")
m = B("m")
h = B("h")
f = B("f")
e = B("e")
i = A("i")
a = B("a")
d = B("d")

n.p = f
m.u = n
m.q = f
h.u = i
h.q = e
f.u = i
f.q = h
e.u = n
e.q = m
i.p = h
a.u = i
a.q = m
d.u = i
d.q = m
assert h.u.p.q.u.p.txt == "f"