Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict


def solve(row, col, d):
    x_map = defaultdict()

    for i in range(row):
        a_i = list(map(int, input().split()))
        for j in range(col):
            x_map[a_i[j]-1] = (i, j)

#    print(x_map)

    q = int(input())

    mp = 0
    for _ in range(q):
        l, r = map(int, input().split())
        for current_pos in range(l+d-1, r, d):
            cp = x_map[current_pos]
            pp = x_map[current_pos-d]
#            print("curre_pos : {0}".format(current_pos))
#            print("cp : {0}".format(cp))
#            print("pp : {0}".format(pp))
            mp += abs(cp[1] - pp[1]) + abs(cp[0] - pp[0])
        print(mp)
        mp = 0


if __name__ == '__main__':
    row, col, d = map(int, input().split())
    solve(row, col, d)


Fixed submission:
from collections import defaultdict


def solve(row, col, d):
    x_map = defaultdict()

    for i in range(row):
        a_i = list(map(int, input().split()))
        for j in range(col):
            x_map[a_i[j]] = (i, j)

    q = int(input())

    dp = [0]*(row*col+1)
    for j in range(d+1, row*col+1):
        x_map_y = abs(x_map[j][0] - x_map[j-d][0])
        x_map_x = abs(x_map[j][1] - x_map[j-d][1])
        dp[j] = dp[j-d]+x_map_y+x_map_x
#        print("j {0} x_map_y : {1} x_map_x : {2} dp[j] : {3}".format(
#            j, x_map_y, x_map_x, dp[j]))

    for _ in range(q):
        l, r = map(int, input().split())
        print(dp[r]-dp[l])


if __name__ == '__main__':
    row, col, d = map(int, input().split())
    solve(row, col, d)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from numpy import array


def solve(row, col, d):
    x_map = defaultdict()

    for i in range(row):
        a_i = list(map(int, input().split()))
        for j in range(col):
            x_map[a_i[j]-1] = (i, j)

#    print(x_map)

    q = int(input())

    mp = 0
    ans = []
    for _ in range(q):
        l, r = map(int, input().split())
        for current_pos in range(l+d-1, r, d):
            cp = x_map[current_pos]
            pp = x_map[current_pos-d]
#            print("curre_pos : {0}".format(current_pos))
#            print("cp : {0}".format(cp))
#            print("pp : {0}".format(pp))
            mp += abs(cp[1] - pp[1]) + abs(cp[0] - pp[0])
        ans.append(mp)
        mp = 0

    ans = array(ans)

    for k in range(q):
        print(ans[k])


if __name__ == '__main__':
    row, col, d = map(int, input().split())
    solve(row, col, d)


Fixed submission:
from collections import defaultdict


def solve(row, col, d):
    x_map = defaultdict()

    for i in range(row):
        a_i = list(map(int, input().split()))
        for j in range(col):
            x_map[a_i[j]] = (i, j)

    q = int(input())

    dp = [0]*(row*col+1)
    for j in range(d+1, row*col+1):
        x_map_y = abs(x_map[j][0] - x_map[j-d][0])
        x_map_x = abs(x_map[j][1] - x_map[j-d][1])
        dp[j] = dp[j-d]+x_map_y+x_map_x
#        print("j {0} x_map_y : {1} x_map_x : {2} dp[j] : {3}".format(
#            j, x_map_y, x_map_x, dp[j]))

    for _ in range(q):
        l, r = map(int, input().split())
        print(dp[r]-dp[l])


if __name__ == '__main__':
    row, col, d = map(int, input().split())
    solve(row, col, d)

Buggy python submission (verdict: Runtime Error):
def solve(row, col, d):
    x_map = [0]*(row**2)

    for i in range(row):
        a_i = list(map(int, input().split()))
        for j in range(col):
            x_map[a_i[j]-1] = (i, j)

#    for k in range(row):
#        print(x_map[k*row:(k+1)*row])
#
    q = int(input())

    mp = 0
    for _ in range(q):
        l, r = map(int, input().split())
        for current_pos in range(l+d-1, r, d):
#            print("curre_pos : {0}".format(current_pos))
#            print("cp : {0}".format(x_map[current_pos]))
#            print("pp : {0}".format(x_map[current_pos-d]))
            cp = x_map[current_pos]
            pp = x_map[current_pos-d]
            mp += abs(cp[1] - pp[1]) + abs(cp[0] - pp[0])
        print(mp)
        mp = 0


if __name__ == '__main__':
    row, col, d = map(int, input().split())
    solve(row, col, d)


Fixed submission:
from collections import defaultdict


def solve(row, col, d):
    x_map = defaultdict()

    for i in range(row):
        a_i = list(map(int, input().split()))
        for j in range(col):
            x_map[a_i[j]] = (i, j)

    q = int(input())

    dp = [0]*(row*col+1)
    for j in range(d+1, row*col+1):
        x_map_y = abs(x_map[j][0] - x_map[j-d][0])
        x_map_x = abs(x_map[j][1] - x_map[j-d][1])
        dp[j] = dp[j-d]+x_map_y+x_map_x
#        print("j {0} x_map_y : {1} x_map_x : {2} dp[j] : {3}".format(
#            j, x_map_y, x_map_x, dp[j]))

    for _ in range(q):
        l, r = map(int, input().split())
        print(dp[r]-dp[l])


if __name__ == '__main__':
    row, col, d = map(int, input().split())
    solve(row, col, d)