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

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

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

c = A("c")
m = B("m")
h = C("h")
d = B("d")

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

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

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

c = A("c")
m = B("m")
h = C("h")
d = B("d")

c.u = m
c.p = d
c.x = h
m.w = d
m.q = h
h.w = c
h.q = c
h.p = d
d.w = m
d.q = h
assert h.p.q.p.txt == "d"