Buggy python submission (verdict: Runtime Error):
h, w, d = map(int, input().split(' '))
q = int(input())
lr = [map(int, input().split(' ')) for _ in range(q)]

comp = {}
for i in range(h):
    tmp = list(map(int, input().split(' ')))
    for j in range(w):
        comp[tmp[j]] = [i, j]

dp = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
    cnt = dp[i - d]
    cnt += abs(comp[i][0] - comp[i - d][0])
    cnt += abs(comp[i][1] - comp[i - d][1])    
    dp[i] = cnt
    

for l, r in lr:
    print(dp[r] - dp[l])

Fixed submission:
h, w, d = map(int, input().split(' '))

comp = {}
for i in range(h):
    tmp = list(map(int, input().split(' ')))
    for j in range(w):
        comp[tmp[j]] = [i, j]

q = int(input())
lr = [map(int, input().split(' ')) for _ in range(q)]

dp = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
    cnt = dp[i - d]
    cnt += abs(comp[i][0] - comp[i - d][0])
    cnt += abs(comp[i][1] - comp[i - d][1])    
    dp[i] = cnt
    

for l, r in lr:
    print(dp[r] - dp[l])

Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split(' '))

comp = {}
for i in range(h):
    tmp = list(map(int, input().split(' ')))
    for j in range(w):
        comp[tmp[j]] = [i, j]
        
q = int(input())
for _ in range(q):
    l, r = map(int, input().split(' '))
    cnt = 0
    tmp = []
    if l != r:
        for k in range(int((r - l) / d) + 1):
            tmp.append(comp[l + d * k])
        for i in range(len(tmp)):
            if i >= 1:
                cnt += abs(tmp[i][0] - tmp[i - 1][0]) + abs(tmp[i][1] - tmp[i - 1][1])

    print(cnt)

Fixed submission:
h, w, d = map(int, input().split(' '))

comp = {}
for i in range(h):
    tmp = list(map(int, input().split(' ')))
    for j in range(w):
        comp[tmp[j]] = [i, j]

q = int(input())
lr = [map(int, input().split(' ')) for _ in range(q)]

dp = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
    cnt = dp[i - d]
    cnt += abs(comp[i][0] - comp[i - d][0])
    cnt += abs(comp[i][1] - comp[i - d][1])    
    dp[i] = cnt
    

for l, r in lr:
    print(dp[r] - dp[l])

Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split(' '))

comp = {}
for i in range(h):
    tmp = list(map(int, input().split(' ')))
    for j in range(w):
        comp[tmp[j]] = [i, j]
        
q = int(input())
for _ in range(q):
    l, r = map(int, input().split(' '))
    cnt = 0
    x, y = comp[l]
    while l != r:
        l += d
        i, j = comp[l]
        cnt += abs(x - i) + abs(y - j)
        x, y = i, j

    print(cnt)

Fixed submission:
h, w, d = map(int, input().split(' '))

comp = {}
for i in range(h):
    tmp = list(map(int, input().split(' ')))
    for j in range(w):
        comp[tmp[j]] = [i, j]

q = int(input())
lr = [map(int, input().split(' ')) for _ in range(q)]

dp = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
    cnt = dp[i - d]
    cnt += abs(comp[i][0] - comp[i - d][0])
    cnt += abs(comp[i][1] - comp[i - d][1])    
    dp[i] = cnt
    

for l, r in lr:
    print(dp[r] - dp[l])

Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split(' '))

comp = {}
for i in range(h):
    matrix = list(map(int, input().split(' ')))
    for j in range(w):
        comp[matrix[j]] = [i, j]
        
q = int(input())
lr = [list(map(int, input().split(' '))) for _ in range(q)]


for l, r in lr:
    cnt = 0
    x, y = comp[l]
                
    while l != r:
        l = l + d
        i, j = comp[l]
        cnt += abs(x - i) + abs(y - j)
        x, y = i, j
                    
    print(cnt)

Fixed submission:
h, w, d = map(int, input().split(' '))

comp = {}
for i in range(h):
    tmp = list(map(int, input().split(' ')))
    for j in range(w):
        comp[tmp[j]] = [i, j]

q = int(input())
lr = [map(int, input().split(' ')) for _ in range(q)]

dp = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
    cnt = dp[i - d]
    cnt += abs(comp[i][0] - comp[i - d][0])
    cnt += abs(comp[i][1] - comp[i - d][1])    
    dp[i] = cnt
    

for l, r in lr:
    print(dp[r] - dp[l])

Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split(' '))
matrix = [list(map(int, input().split(' '))) for _ in range(h)]
map2 = {}
for i in range(h):
    for j in range(w):
        map2[matrix[i][j]] = [i, j]
        
q = int(input())
lr = [list(map(int, input().split(' '))) for _ in range(q)]


for l, r in lr:
    cnt = 0
    x, y = map2[l]
                
    while l != r:
        l = l + d
        i, j = map2[l]
        cnt += abs(x - i) + abs(y - j)
        x = i
        y = j
                    
    print(cnt)

Fixed submission:
h, w, d = map(int, input().split(' '))

comp = {}
for i in range(h):
    tmp = list(map(int, input().split(' ')))
    for j in range(w):
        comp[tmp[j]] = [i, j]

q = int(input())
lr = [map(int, input().split(' ')) for _ in range(q)]

dp = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
    cnt = dp[i - d]
    cnt += abs(comp[i][0] - comp[i - d][0])
    cnt += abs(comp[i][1] - comp[i - d][1])    
    dp[i] = cnt
    

for l, r in lr:
    print(dp[r] - dp[l])

Buggy python submission (verdict: Time Limit Exceeded):
h, w, d = map(int, input().split(' '))
matrix = [list(map(int, input().split(' '))) for _ in range(h)]
q = int(input())
lr = [list(map(int, input().split(' '))) for _ in range(q)]


for l, r in lr:
    cnt = 0
    for i in range(h):
        for j in range(w):
            if matrix[i][j] == l:
                x = i
                y = j
                
    while l != r:
        l = l + d
        for i in range(h):
            for j in range(w):
                if matrix[i][j] == l:
                    cnt += abs(x - i) + abs(y - j)
                    x = i
                    y = j
                    
    print(cnt)

Fixed submission:
h, w, d = map(int, input().split(' '))

comp = {}
for i in range(h):
    tmp = list(map(int, input().split(' ')))
    for j in range(w):
        comp[tmp[j]] = [i, j]

q = int(input())
lr = [map(int, input().split(' ')) for _ in range(q)]

dp = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
    cnt = dp[i - d]
    cnt += abs(comp[i][0] - comp[i - d][0])
    cnt += abs(comp[i][1] - comp[i - d][1])    
    dp[i] = cnt
    

for l, r in lr:
    print(dp[r] - dp[l])