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

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

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

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

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

c.x = d
m.r = c
d.y = h
d.p = k
k.y = m
k.z = m
h.r = c
assert k.z.r.x.p.y.txt == "
class A:
    def __init__(self, txt: str):
        self.x: C = None
        self.txt = txt

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

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

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

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

c.x = d
m.r = c
d.y = h
d.p = k
k.y = m
k.z = m
h.r = c
assert k.z.r.x.p.y.txt == "m"