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

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

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

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

a = A("a")
b = B("b")
c = C("c")
g = D("g")

a.r = b
b.t = g
b.w = c
c.r = a
c.v = b
g.t = c
g.s = b
g.p = b
assert g.p.t.t.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.txt = txt

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

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

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

a = A("a")
b = B("b")
c = C("c")
g = D("g")

a.r = b
b.t = g
b.w = c
c.r = a
c.v = b
g.t = c
g.s = b
g.p = b
assert g.p.t.t.txt == "c"