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

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

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

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

h = A("h")
d = B("d")
m = C("m")
b = D("b")

h.p = d
h.w = d
d.t = b
m.x = d
b.r = m
b.q = m
b.x = h
b.p = d
assert m.x.t.r.x.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.w: B = None
        self.txt = txt

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

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

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

h = A("h")
d = B("d")
m = C("m")
b = D("b")

h.p = d
h.w = d
d.t = b
m.x = d
b.r = m
b.q = m
b.x = h
b.p = d
assert m.x.t.r.x.txt == "d"