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

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

n = A("n")
k = B("k")
f = B("f")
i = B("i")
g = A("g")
h = B("h")
m = B("m")
b = B("b")

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

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

n = A("n")
k = B("k")
f = B("f")
i = B("i")
g = A("g")
h = B("h")
m = B("m")
b = B("b")

n.z = g
k.q = h
f.q = h
i.q = k
g.z = n
h.q = k
m.q = h
b.q = k
assert n.z.z.z.z.txt == "n"