Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split())
A = []
for _ in range(h):
    a = list(map(int, input().split()))
    A.append(a)

q = int(input())
LR = []
for _ in range(q):
    l, r = map(int, input().split())
    LR.append([l,r])

Inds = {x:[] for x in range(1, h*w+1)}
for j in range(h):
    for k in range(w):
        Inds[A[j][k]].append(j)
        Inds[A[j][k]].append(k)
# print(Inds)

for lr in LR:
    l = lr[0]
    r = lr[1]
    now = l
    ans = 0
    while now < r:
        # print('y is' + str(Inds[now-1][0]))
        # print('x is' + str(Inds[now-1][1]))
        ans += abs(Inds[now][0]-Inds[now+d][0]) + abs(Inds[now][1]-Inds[now+d][1])
        now += d
    print(ans)

Fixed submission:
h, w, d = map(int, input().split())
A = []
for _ in range(h):
    a = list(map(int, input().split()))
    A.append(a)

q = int(input())
LR = []
for _ in range(q):
    l, r = map(int, input().split())
    LR.append([l,r])

Inds = {x:[] for x in range(1, h*w+1)}
for j in range(h):
    for k in range(w):
        Inds[A[j][k]].append(j)
        Inds[A[j][k]].append(k)

Dist = [-1 for _ in range(h*w+1)]
for i in range(1,h*w+1):
    if i <= d:
        Dist[i] = 0
    else:
        Dist[i] = Dist[i-d] + abs(Inds[i][0]-Inds[i-d][0]) + abs(Inds[i][1]-Inds[i-d][1])

for lr in LR:
    ans = Dist[lr[1]] - Dist[lr[0]]
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split())
A = []
for _ in range(h):
    a = list(map(int, input().split()))
    A.append(a)

q = int(input())
LR = []
for _ in range(q):
    l, r = map(int, input().split())
    LR.append([l,r])

Inds = {x:[] for x in range(1, h*w+1)}
for j in range(h):
    for k in range(w):
        Inds[A[j][k]].append(j)
        Inds[A[j][k]].append(k)
# print(Inds)

for lr in LR:
    l = lr[0]
    r = lr[1]
    now = l
    ans = 0
    while now < r:
        # print('y is' + str(Inds[now-1][0]))
        # print('x is' + str(Inds[now-1][1]))
        ans += abs(Inds[now][0]-Inds[now+d][0]) + abs(Inds[now][1]-Inds[now+d][1])
        now += d
    print(ans)

Fixed submission:
h, w, d = map(int, input().split())
A = []
for _ in range(h):
    a = list(map(int, input().split()))
    A.append(a)

q = int(input())
LR = []
for _ in range(q):
    l, r = map(int, input().split())
    LR.append([l,r])

Inds = {x:[] for x in range(1, h*w+1)}
for j in range(h):
    for k in range(w):
        Inds[A[j][k]].append(j)
        Inds[A[j][k]].append(k)

Dist = [-1 for _ in range(h*w+1)]
for i in range(1,h*w+1):
    if i <= d:
        Dist[i] = 0
    else:
        Dist[i] = Dist[i-d] + abs(Inds[i][0]-Inds[i-d][0]) + abs(Inds[i][1]-Inds[i-d][1])

for lr in LR:
    ans = Dist[lr[1]] - Dist[lr[0]]
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split())
A = []
for _ in range(h):
    a = list(map(int, input().split()))
    A.append(a)

q = int(input())
LR = []
for _ in range(q):
    l, r = map(int, input().split())
    LR.append([l,r])

Inds = []
for i in range(h*w):
    for j in range(h):
        for k in range(w):
            if A[j][k] == i+1:
                Inds.append([j,k])
# print(Inds)

for lr in LR:
    l = lr[0]
    r = lr[1]
    now = l
    ans = 0
    while now < r:
        # print('y is' + str(Inds[now-1][0]))
        # print('x is' + str(Inds[now-1][1]))
        ans += abs(Inds[now-1][0]-Inds[now+d-1][0]) + abs(Inds[now-1][1]-Inds[now+d-1][1])
        now += d
    print(ans)

Fixed submission:
h, w, d = map(int, input().split())
A = []
for _ in range(h):
    a = list(map(int, input().split()))
    A.append(a)

q = int(input())
LR = []
for _ in range(q):
    l, r = map(int, input().split())
    LR.append([l,r])

Inds = {x:[] for x in range(1, h*w+1)}
for j in range(h):
    for k in range(w):
        Inds[A[j][k]].append(j)
        Inds[A[j][k]].append(k)

Dist = [-1 for _ in range(h*w+1)]
for i in range(1,h*w+1):
    if i <= d:
        Dist[i] = 0
    else:
        Dist[i] = Dist[i-d] + abs(Inds[i][0]-Inds[i-d][0]) + abs(Inds[i][1]-Inds[i-d][1])

for lr in LR:
    ans = Dist[lr[1]] - Dist[lr[0]]
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split())
A = []
for _ in range(h):
    a = list(map(int, input().split()))
    A.append(a)

q = int(input())
LR = []
for _ in range(q):
    l, r = map(int, input().split())
    LR.append([l,r])

Inds = []
for i in range(h*w):
    for j in range(h):
        for k in range(w):
            if A[j][k] == i+1:
                Inds.append([j,k])
# print(Inds)

for lr in LR:
    l = lr[0]
    r = lr[1]
    now = l
    ans = 0
    while now < r:
        # print('y is' + str(Inds[now-1][0]))
        # print('x is' + str(Inds[now-1][1]))
        ans += abs(Inds[now-1][0]-Inds[now+d-1][0]) + abs(Inds[now-1][1]-Inds[now+d-1][1])
        now += d
    print(ans)

Fixed submission:
h, w, d = map(int, input().split())
A = []
for _ in range(h):
    a = list(map(int, input().split()))
    A.append(a)

q = int(input())
LR = []
for _ in range(q):
    l, r = map(int, input().split())
    LR.append([l,r])

Inds = {x:[] for x in range(1, h*w+1)}
for j in range(h):
    for k in range(w):
        Inds[A[j][k]].append(j)
        Inds[A[j][k]].append(k)

Dist = [-1 for _ in range(h*w+1)]
for i in range(1,h*w+1):
    if i <= d:
        Dist[i] = 0
    else:
        Dist[i] = Dist[i-d] + abs(Inds[i][0]-Inds[i-d][0]) + abs(Inds[i][1]-Inds[i-d][1])

for lr in LR:
    ans = Dist[lr[1]] - Dist[lr[0]]
    print(ans)