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

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

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

g = A("g")
a = B("a")
k = C("k")
d = A("d")
n = B("n")
m = C("m")

g.z = m
a.p = g
a.w = m
k.z = a
d.z = k
n.p = g
n.w = k
m.z = a
assert d.z.z.p.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.z: C = None
        self.txt = txt

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

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

g = A("g")
a = B("a")
k = C("k")
d = A("d")
n = B("n")
m = C("m")

g.z = m
a.p = g
a.w = m
k.z = a
d.z = k
n.p = g
n.w = k
m.z = a
assert d.z.z.p.z.z.txt == "a"