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

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

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

h = A("h")
d = B("d")
i = C("i")
b = C("b")
m = A("m")

h.q = d
h.z = d
d.s = b
d.o = b
d.r = h
i.r = b
b.r = i
m.q = d
m.z = d
assert d.r.z.r.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.z: B = None
        self.txt = txt

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

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

h = A("h")
d = B("d")
i = C("i")
b = C("b")
m = A("m")

h.q = d
h.z = d
d.s = b
d.o = b
d.r = h
i.r = b
b.r = i
m.q = d
m.z = d
assert d.r.z.r.txt == "h"