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

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

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

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

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

a.u = m
a.p = k
m.u = g
m.w = b
m.s = k
g.r = k
k.p = g
k.s = g
k.u = g
b.u = m
b.p = k
assert m.s.s.r.s.txt == "
class A:
    def __init__(self, txt: str):
        self.u: B = None
        self.p: D = None
        self.txt = txt

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

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

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

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

a.u = m
a.p = k
m.u = g
m.w = b
m.s = k
g.r = k
k.p = g
k.s = g
k.u = g
b.u = m
b.p = k
assert m.s.s.r.s.txt == "g"