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

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

h = A("h")
n = B("n")
m = B("m")
f = B("f")
e = A("e")
g = B("g")
j = B("j")
a = B("a")

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

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

h = A("h")
n = B("n")
m = B("m")
f = B("f")
e = A("e")
g = B("g")
j = B("j")
a = B("a")

h.u = e
h.p = j
n.q = a
m.q = n
f.q = a
e.u = h
e.p = n
g.q = n
j.q = m
a.q = f
assert g.q.q.q.q.q.q.txt == "a"