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

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

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

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

a = A("a")
b = B("b")
k = C("k")
f = D("f")
n = D("n")
g = B("g")

a.u = k
a.z = k
a.x = f
b.p = k
k.y = b
f.u = a
f.p = a
n.u = a
n.p = a
g.p = k
assert g.p.y.p.txt == "
class A:
    def __init__(self, txt: str):
        self.u: C = None
        self.z: C = None
        self.x: D = None
        self.txt = txt

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

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

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

a = A("a")
b = B("b")
k = C("k")
f = D("f")
n = D("n")
g = B("g")

a.u = k
a.z = k
a.x = f
b.p = k
k.y = b
f.u = a
f.p = a
n.u = a
n.p = a
g.p = k
assert g.p.y.p.txt == "k"