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

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

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

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

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

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

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

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

h.y = m
m.p = d
m.y = h
g.y = h
g.q = h
g.o = h
d.y = h
d.q = h
d.o = h
assert g.q.y.p.txt == "d"