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

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

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

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

d = A("d")
b = B("b")
g = C("g")
l = D("l")

d.z = g
b.w = g
b.p = g
b.r = d
g.r = d
l.s = g
l.w = b
assert g.r.z.r.z.txt == "
class A:
    def __init__(self, txt: str):
        self.z: C = None
        self.txt = txt

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

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

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

d = A("d")
b = B("b")
g = C("g")
l = D("l")

d.z = g
b.w = g
b.p = g
b.r = d
g.r = d
l.s = g
l.w = b
assert g.r.z.r.z.txt == "g"