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

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

i = A("i")
m = B("m")
f = A("f")
c = B("c")
n = A("n")
h = A("h")

i.r = h
m.q = c
m.z = f
f.r = n
c.q = m
c.z = i
n.r = h
h.r = i
assert f.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.txt = txt

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

i = A("i")
m = B("m")
f = A("f")
c = B("c")
n = A("n")
h = A("h")

i.r = h
m.q = c
m.z = f
f.r = n
c.q = m
c.z = i
n.r = h
h.r = i
assert f.r.r.r.txt == "i"