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

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

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

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

h = A("h")
a = B("a")
f = C("f")
m = D("m")

h.x = m
h.z = a
a.q = f
a.r = m
f.p = a
f.x = a
m.y = h
m.r = f
assert h.x.y.x.txt == "
class A:
    def __init__(self, txt: str):
        self.x: D = None
        self.z: B = None
        self.txt = txt

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

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

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

h = A("h")
a = B("a")
f = C("f")
m = D("m")

h.x = m
h.z = a
a.q = f
a.r = m
f.p = a
f.x = a
m.y = h
m.r = f
assert h.x.y.x.txt == "m"