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

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

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

g = A("g")
a = B("a")
i = C("i")
n = A("n")
m = B("m")
b = C("b")

g.q = n
a.s = i
a.z = i
a.p = g
i.r = g
n.q = g
m.s = b
m.p = n
m.z = i
b.r = n
assert a.s.r.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.txt = txt

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

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

g = A("g")
a = B("a")
i = C("i")
n = A("n")
m = B("m")
b = C("b")

g.q = n
a.s = i
a.z = i
a.p = g
i.r = g
n.q = g
m.s = b
m.p = n
m.z = i
b.r = n
assert a.s.r.q.q.q.txt == "n"