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

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

k = A("k")
e = B("e")
m = A("m")
a = B("a")
b = B("b")
i = B("i")

k.q = m
e.q = m
e.p = k
m.q = k
a.q = k
a.p = m
b.q = k
b.p = m
i.q = k
i.p = k
assert a.p.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.txt = txt

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

k = A("k")
e = B("e")
m = A("m")
a = B("a")
b = B("b")
i = B("i")

k.q = m
e.q = m
e.p = k
m.q = k
a.q = k
a.p = m
b.q = k
b.p = m
i.q = k
i.p = k
assert a.p.q.q.q.txt == "k"