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

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

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

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

d = A("d")
k = B("k")
f = C("f")
g = D("g")
b = A("b")

d.x = b
d.s = g
k.z = g
f.z = d
f.w = g
g.u = k
g.q = k
g.x = d
b.x = d
b.s = g
assert k.z.x.x.s.txt == "
class A:
    def __init__(self, txt: str):
        self.x: A = None
        self.s: D = None
        self.txt = txt

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

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

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

d = A("d")
k = B("k")
f = C("f")
g = D("g")
b = A("b")

d.x = b
d.s = g
k.z = g
f.z = d
f.w = g
g.u = k
g.q = k
g.x = d
b.x = d
b.s = g
assert k.z.x.x.s.txt == "g"