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

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

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

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

b = A("b")
g = B("g")
m = C("m")
k = D("k")
c = C("c")

b.x = g
g.z = b
g.p = b
g.x = b
g.q = k
m.u = k
m.v = b
m.p = b
m.x = g
k.w = g
c.u = k
c.v = b
c.p = b
c.x = g
assert m.u.w.x.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.txt = txt

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

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

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

b = A("b")
g = B("g")
m = C("m")
k = D("k")
c = C("c")

b.x = g
g.z = b
g.p = b
g.x = b
g.q = k
m.u = k
m.v = b
m.p = b
m.x = g
k.w = g
c.u = k
c.v = b
c.p = b
c.x = g
assert m.u.w.x.txt == "b"