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

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

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

g = A("g")
h = B("h")
e = C("e")
b = B("b")
m = B("m")

g.s = e
h.s = m
h.x = e
h.q = g
e.s = b
e.r = g
b.s = m
b.x = e
b.q = g
m.s = h
m.x = e
m.q = g
assert m.q.s.r.txt == "
class A:
    def __init__(self, txt: str):
        self.s: C = None
        self.txt = txt

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

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

g = A("g")
h = B("h")
e = C("e")
b = B("b")
m = B("m")

g.s = e
h.s = m
h.x = e
h.q = g
e.s = b
e.r = g
b.s = m
b.x = e
b.q = g
m.s = h
m.x = e
m.q = g
assert m.q.s.r.txt == "g"