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

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

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

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

k = A("k")
m = B("m")
g = C("g")
d = D("d")
h = A("h")

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

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

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

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

k = A("k")
m = B("m")
g = C("g")
d = D("d")
h = A("h")

k.p = g
m.x = g
g.z = k
g.q = k
g.s = d
g.w = d
d.u = h
d.s = k
d.q = m
h.p = g
assert k.p.z.p.txt == "g"