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

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

i = A("i")
g = B("g")
m = B("m")
k = A("k")
l = B("l")

i.q = k
g.r = k
m.r = k
k.q = i
l.r = k
assert m.r.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.r: A = None
        self.txt = txt

i = A("i")
g = B("g")
m = B("m")
k = A("k")
l = B("l")

i.q = k
g.r = k
m.r = k
k.q = i
l.r = k
assert m.r.q.q.q.txt == "i"