class A:
    def __init__(self, txt: str):
        self.p: D = 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.z: A = None
        self.txt = txt

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

n = A("n")
h = B("h")
d = C("d")
m = D("m")
l = D("l")

n.p = l
n.x = l
h.p = d
d.z = n
m.x = n
m.q = l
m.w = l
m.y = h
l.x = n
l.q = m
l.w = m
l.y = h
assert h.p.z.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: D = 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.z: A = None
        self.txt = txt

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

n = A("n")
h = B("h")
d = C("d")
m = D("m")
l = D("l")

n.p = l
n.x = l
h.p = d
d.z = n
m.x = n
m.q = l
m.w = l
m.y = h
l.x = n
l.q = m
l.w = m
l.y = h
assert h.p.z.p.txt == "l"