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

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

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

n = A("n")
h = B("h")
i = C("i")
g = B("g")
d = B("d")
k = A("k")

n.z = d
n.q = k
h.p = g
h.q = i
i.x = h
g.p = d
g.q = i
d.p = h
d.q = i
k.z = g
k.q = n
assert g.p.q.x.txt == "
class A:
    def __init__(self, txt: str):
        self.z: B = None
        self.q: A = None
        self.txt = txt

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

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

n = A("n")
h = B("h")
i = C("i")
g = B("g")
d = B("d")
k = A("k")

n.z = d
n.q = k
h.p = g
h.q = i
i.x = h
g.p = d
g.q = i
d.p = h
d.q = i
k.z = g
k.q = n
assert g.p.q.x.txt == "h"