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

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

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

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

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

h.t = m
k.w = h
d.w = k
d.z = m
d.p = h
m.v = d
m.z = h
assert m.z.t.z.t.txt == "
class A:
    def __init__(self, txt: str):
        self.t: D = None
        self.txt = txt

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

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

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

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

h.t = m
k.w = h
d.w = k
d.z = m
d.p = h
m.v = d
m.z = h
assert m.z.t.z.t.txt == "m"