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

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

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

m = A("m")
n = B("n")
g = C("g")
i = A("i")
f = B("f")
h = B("h")

m.p = i
n.w = g
n.s = h
n.q = m
g.p = h
i.p = m
f.w = g
f.s = n
f.q = m
h.w = g
h.s = n
h.q = m
assert f.q.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: A = None
        self.txt = txt

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

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

m = A("m")
n = B("n")
g = C("g")
i = A("i")
f = B("f")
h = B("h")

m.p = i
n.w = g
n.s = h
n.q = m
g.p = h
i.p = m
f.w = g
f.s = n
f.q = m
h.w = g
h.s = n
h.q = m
assert f.q.p.p.txt == "m"