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

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

class C:
    def __init__(self, txt: str):
        self.x: A = None
        self.q: A = None
        self.txt = txt

h = A("h")
g = B("g")
m = C("m")
a = A("a")
k = A("k")
e = C("e")

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

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

class C:
    def __init__(self, txt: str):
        self.x: A = None
        self.q: A = None
        self.txt = txt

h = A("h")
g = B("g")
m = C("m")
a = A("a")
k = A("k")
e = C("e")

h.u = g
g.x = a
g.q = a
g.p = a
m.x = h
m.q = h
a.u = g
k.u = g
e.x = a
e.q = h
assert a.u.q.u.x.u.q.txt == "a"