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

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

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

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

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

a.x = h
m.u = g
m.v = a
d.t = a
h.x = m
h.y = g
h.r = d
g.u = m
g.v = a
k.t = a
assert m.v.x.r.txt == "
class A:
    def __init__(self, txt: str):
        self.x: D = None
        self.txt = txt

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

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

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

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

a.x = h
m.u = g
m.v = a
d.t = a
h.x = m
h.y = g
h.r = d
g.u = m
g.v = a
k.t = a
assert m.v.x.r.txt == "d"