H,W,D=list(map(int,input().split()))

position=[None]*(H*W+1)

for i in range(H):

  A=list(map(int,input().split()))

  for j in range(len(A)):

    position[A[j]]=[i,j]



dp=[0]*(H*W+1)



for i in range(1,len(dp)):

  if i+D>=len(dp):

    continue

  dp[i]=abs(position[i+D][0]-position[i][0])+abs(position[i+D][1]-position[i][1])



dpsum=[0]*(H*W+1)



for i in range(1,len(dp)):

  if i-D>=0:

    dpsum[i]=dpsum[i-D]+dp[i]

  else:

    dpsum[i]=dp[i]



Q=int(eval(input()))

for i in range(Q):

  L,R=list(map(int,input().split()))

  if L==R:

    print((0))

  elif L-D>0:

    print((dpsum[R-D]-dpsum[L-D]))

  else:

    print((dpsum[R-D]))

import sys

readline = sys.stdin.readline



H,W,D = list(map(int,readline().split()))



route = [0] * (H * W + 1)

for i in range(H):

  line = list(map(int,readline().split()))

  for j in range(len(line)):

    route[line[j]] = (i,j)



# 1,2,3 ... の頂点番号を格納した配列を作る

# D個おきに距離を格納する



dist = [0] * (H * W + 1)

for i in range(D + 1,len(route)):

  gy,gx = route[i]

  sy,sx = route[i - D]

  dist[i] = abs(gy - sy) + abs(gx - sx) 

  dist[i] = dist[i] + dist[i - D]





Q = int(readline())



for i in range(Q):

  l,r = list(map(int,readline().split()))

  print((dist[r] - dist[l]))

H, W, D = list(map(int, input().split()))
position = [None] * (H * W + 1)
for i in range(H):
    A = list(map(int, input().split()))
    for j in range(len(A)):
        position[A[j]] = [i, j]
dp = [0] * (H * W + 1)
for i in range(1, len(dp)):
    if i + D >= len(dp):
        continue
    dp[i] = abs(position[i + D][0] - position[i][0]) + abs(
        position[i + D][1] - position[i][1]
    )
dpsum = [0] * (H * W + 1)
for i in range(1, len(dp)):
    if i - D >= 0:
        dpsum[i] = dpsum[i - D] + dp[i]
    else:
        dpsum[i] = dp[i]
Q = int(eval(input()))
for i in range(Q):
    L, R = list(map(int, input().split()))
    if L == R:
        print((0))
    elif L - D > 0:
        print((dpsum[R - D] - dpsum[L - D]))
    else:
        print((dpsum[R - D]))

import sys

readline = sys.stdin.readline
H, W, D = list(map(int, readline().split()))
route = [0] * (H * W + 1)
for i in range(H):
    line = list(map(int, readline().split()))
    for j in range(len(line)):
        route[line[j]] = (i, j)
# 1,2,3 ... の頂点番号を格納した配列を作る
# D個おきに距離を格納する
dist = [0] * (H * W + 1)
for i in range(D + 1, len(route)):
    gy, gx = route[i]
    sy, sx = route[i - D]
    dist[i] = abs(gy - sy) + abs(gx - sx)
    dist[i] = dist[i] + dist[i - D]
Q = int(readline())
for i in range(Q):
    l, r = list(map(int, readline().split()))
    print((dist[r] - dist[l]))

-H, W, D = list(map(int, input().split())) -position = [None] * (H * W + 1) +import sys + +readline = sys.stdin.readline +H, W, D = list(map(int, readline().split())) +route = [0] * (H * W + 1) -    A = list(map(int, input().split())) -    for j in range(len(A)): -        position[A[j]] = [i, j] -dp = [0] * (H * W + 1) -for i in range(1, len(dp)): -    if i + D >= len(dp): -        continue -    dp[i] = abs(position[i + D][0] - position[i][0]) + abs( -        position[i + D][1] - position[i][1] -    ) -dpsum = [0] * (H * W + 1) -for i in range(1, len(dp)): -    if i - D >= 0: -        dpsum[i] = dpsum[i - D] + dp[i] -    else: -        dpsum[i] = dp[i] -Q = int(eval(input())) +    line = list(map(int, readline().split())) +    for j in range(len(line)): +        route[line[j]] = (i, j) +# 1,2,3 ... の頂点番号を格納した配列を作る +# D個おきに距離を格納する +dist = [0] * (H * W + 1) +for i in range(D + 1, len(route)): +    gy, gx = route[i] +    sy, sx = route[i - D] +    dist[i] = abs(gy - sy) + abs(gx - sx) +    dist[i] = dist[i] + dist[i - D] +Q = int(readline()) -    L, R = list(map(int, input().split())) -    if L == R: -        print((0)) -    elif L - D > 0: -        print((dpsum[R - D] - dpsum[L - D])) -    else: -        print((dpsum[R - D])) +    l, r = list(map(int, readline().split())) +    print((dist[r] - dist[l]))