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

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

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

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

g = A("g")
f = B("f")
n = C("n")
d = D("d")
c = B("c")
i = A("i")

g.r = d
f.x = g
f.q = n
n.x = g
d.v = i
c.x = g
c.q = n
i.r = d
assert g.r.v.r.v.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: D = None
        self.txt = txt

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

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

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

g = A("g")
f = B("f")
n = C("n")
d = D("d")
c = B("c")
i = A("i")

g.r = d
f.x = g
f.q = n
n.x = g
d.v = i
c.x = g
c.q = n
i.r = d
assert g.r.v.r.v.r.txt == "d"