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

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

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

a = A("a")
n = B("n")
m = C("m")
c = B("c")
b = B("b")

a.q = c
n.p = m
n.u = a
m.p = c
m.r = a
m.q = a
c.p = m
c.u = a
b.p = m
b.u = a
assert n.p.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.txt = txt

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

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

a = A("a")
n = B("n")
m = C("m")
c = B("c")
b = B("b")

a.q = c
n.p = m
n.u = a
m.p = c
m.r = a
m.q = a
c.p = m
c.u = a
b.p = m
b.u = a
assert n.p.p.p.txt == "m"