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

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

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

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

i = A("i")
g = B("g")
m = C("m")
l = D("l")

i.w = g
g.w = l
g.p = i
m.r = g
l.y = m
l.z = i
l.w = i
l.p = g
assert g.p.w.p.w.txt == "
class A:
    def __init__(self, txt: str):
        self.w: B = None
        self.txt = txt

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

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

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

i = A("i")
g = B("g")
m = C("m")
l = D("l")

i.w = g
g.w = l
g.p = i
m.r = g
l.y = m
l.z = i
l.w = i
l.p = g
assert g.p.w.p.w.txt == "g"