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

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

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

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

d = A("d")
c = B("c")
a = C("a")
m = D("m")

d.x = m
c.q = d
a.q = d
a.w = m
m.w = c
assert c.q.x.w.q.txt == "
class A:
    def __init__(self, txt: str):
        self.x: D = None
        self.txt = txt

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

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

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

d = A("d")
c = B("c")
a = C("a")
m = D("m")

d.x = m
c.q = d
a.q = d
a.w = m
m.w = c
assert c.q.x.w.q.txt == "d"