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

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

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

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

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

k.u = f
k.r = m
f.y = k
f.z = k
f.p = g
c.z = g
m.y = k
g.z = c
h.z = g
assert m.y.u.y.u.z.r.txt == "
class A:
    def __init__(self, txt: str):
        self.u: B = None
        self.r: D = None
        self.txt = txt

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

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

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

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

k.u = f
k.r = m
f.y = k
f.z = k
f.p = g
c.z = g
m.y = k
g.z = c
h.z = g
assert m.y.u.y.u.z.r.txt == "m"