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

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

g = A("g")
f = B("f")
i = A("i")
m = A("m")
h = B("h")
c = B("c")
a = B("a")

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

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

g = A("g")
f = B("f")
i = A("i")
m = A("m")
h = B("h")
c = B("c")
a = B("a")

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