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

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

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

m = A("m")
h = B("h")
d = C("d")
g = B("g")
i = A("i")

m.q = i
h.p = g
d.r = h
d.p = m
g.p = h
i.q = m
assert g.p.p.p.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.txt = txt

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

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

m = A("m")
h = B("h")
d = C("d")
g = B("g")
i = A("i")

m.q = i
h.p = g
d.r = h
d.p = m
g.p = h
i.q = m
assert g.p.p.p.p.p.txt == "h"