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

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

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

h = A("h")
j = B("j")
g = C("g")
f = A("f")
m = C("m")

h.q = j
h.s = m
h.p = f
j.s = g
g.v = m
g.r = h
f.q = j
f.s = g
f.p = h
m.v = g
m.r = h
assert g.v.v.v.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.s: C = None
        self.p: A = None
        self.txt = txt

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

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

h = A("h")
j = B("j")
g = C("g")
f = A("f")
m = C("m")

h.q = j
h.s = m
h.p = f
j.s = g
g.v = m
g.r = h
f.q = j
f.s = g
f.p = h
m.v = g
m.r = h
assert g.v.v.v.txt == "m"