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

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

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

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

m = A("m")
f = B("f")
b = C("b")
c = D("c")

m.q = c
m.p = c
f.z = m
b.q = c
b.p = c
b.w = m
b.u = f
c.z = m
assert b.p.z.q.z.txt == "
class A:
    def __init__(self, txt: str):
        self.q: D = None
        self.p: D = None
        self.txt = txt

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

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

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

m = A("m")
f = B("f")
b = C("b")
c = D("c")

m.q = c
m.p = c
f.z = m
b.q = c
b.p = c
b.w = m
b.u = f
c.z = m
assert b.p.z.q.z.txt == "m"