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

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

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

a = A("a")
b = B("b")
h = C("h")
m = C("m")
g = C("g")
c = B("c")

a.p = m
b.y = a
b.p = m
b.r = c
h.r = b
m.r = b
g.r = c
c.y = a
c.p = g
c.r = b
assert h.r.p.r.y.txt == "
class A:
    def __init__(self, txt: str):
        self.p: C = None
        self.txt = txt

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

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

a = A("a")
b = B("b")
h = C("h")
m = C("m")
g = C("g")
c = B("c")

a.p = m
b.y = a
b.p = m
b.r = c
h.r = b
m.r = b
g.r = c
c.y = a
c.p = g
c.r = b
assert h.r.p.r.y.txt == "a"