Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

def one_game(i, D, A, start, end):
    current = np.array([])
    count = 0
    for X in range(start, end+1, D):
        old = np.copy(current)
        current = np.where(A==X)
        if X == start:
            continue
        count += abs(current[0][0] - old[0][0]) + abs(current[1][0] - old[1][0])
    return count

def f(H, W, D, A, Q, ST):
    A = np.array(A)
    counts = []
    for i in range(Q):
        start = ST[i][0]
        end = ST[i][1]
        count = one_game(i, D, A, start, end)
        counts.append(count)
    return counts

def main():
    H, W, D = map(int, input().split())
    A = [
        list(map(int, input().split())) for _ in range(H)
    ]
    Q = int(input())
    ST = [
        list(map(int, input().split())) for _ in range(Q)
    ]

    counts = f(H, W, D, A, Q, ST)
    for i in range(Q):
        print(counts[i])

if __name__ == '__main__':
    main()


Fixed submission:
def main():
    H, W, D = map(int, input().split())
    A = [
        list(map(int, input().split())) for _ in range(H)
    ]
    Q = int(input())
    ST = [
        list(map(int, input().split())) for _ in range(Q)
    ]

    pos = [0] * (H * W + 1)
    for i, row in enumerate(A):
        for j, value in enumerate(row):
            pos[value] = (i, j)

    d = [0] * (H * W + 1)
    for x in range(D + 1, H * W + 1):
        y = x - D
        p1, p2 = pos[x], pos[y]
        d[x] = d[y] + abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])

    for l, r in ST:
        print(d[r] - d[l])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

def count():
    pass

def one_game(i, D, A, start, end):
    current = np.array([])
    count = 0
    for X in range(start, end+1, D):
        old = np.copy(current)
        current = np.where(A==X)
        if X == start:
            continue
        count += abs(current[0][0] - old[0][0]) + abs(current[1][0] - old[1][0])
    return count

def f(H, W, D, A, Q, ST):
    A = np.array(A)
    counts = []
    for i in range(Q):
        start = ST[i][0]
        end = ST[i][1]
        count = one_game(i, D, A, start, end)
        counts.append(count)
    return counts

def main():
    H, W, D = map(int, input().split())
    A = [
        list(map(int, input().split())) for _ in range(H)
    ]
    Q = int(input())
    ST = [
        list(map(int, input().split())) for _ in range(Q)
    ]

    counts = f(H, W, D, A, Q, ST)
    for i in range(Q):
        print(counts[i])

if __name__ == '__main__':
    main()


Fixed submission:
def main():
    H, W, D = map(int, input().split())
    A = [
        list(map(int, input().split())) for _ in range(H)
    ]
    Q = int(input())
    ST = [
        list(map(int, input().split())) for _ in range(Q)
    ]

    pos = [0] * (H * W + 1)
    for i, row in enumerate(A):
        for j, value in enumerate(row):
            pos[value] = (i, j)

    d = [0] * (H * W + 1)
    for x in range(D + 1, H * W + 1):
        y = x - D
        p1, p2 = pos[x], pos[y]
        d[x] = d[y] + abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])

    for l, r in ST:
        print(d[r] - d[l])


if __name__ == '__main__':
    main()