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

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

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

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

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

a.t = d
m.t = a
m.o = a
m.s = b
b.r = m
k.w = d
k.y = m
d.t = a
assert b.r.t.t.txt == "
class A:
    def __init__(self, txt: str):
        self.t: A = None
        self.txt = txt

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

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

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

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

a.t = d
m.t = a
m.o = a
m.s = b
b.r = m
k.w = d
k.y = m
d.t = a
assert b.r.t.t.txt == "d"