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

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

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

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

c = A("c")
h = B("h")
m = C("m")
g = D("g")
k = A("k")

c.q = g
h.u = m
m.u = k
g.q = h
g.z = c
g.r = m
k.q = g
assert h.u.u.q.z.q.txt == "
class A:
    def __init__(self, txt: str):
        self.q: D = None
        self.txt = txt

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

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

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

c = A("c")
h = B("h")
m = C("m")
g = D("g")
k = A("k")

c.q = g
h.u = m
m.u = k
g.q = h
g.z = c
g.r = m
k.q = g
assert h.u.u.q.z.q.txt == "g"