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

dist = [0]*(H*W+1)#dist[i]はiからi+Dまでの距離

pos = [[0,0] for i in range(H*W+1)]

for i in range(H):

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

  for j in range(W):

    pos[A[j]] = [i+1, j+1]

for i in range(H*W+1-D):

  dist[i] = abs(pos[i][0]-pos[i+D][0])+abs(pos[i][1]-pos[i+D][1])

distances = [[] for i in range(D)]#あまりがiの距離

for i in range(1,H*W+1-D):

  d = dist[i]

  distances[i%D]+=[d]

distances2 = [[0] for i in range(D)]#累積和に直す

for i in range(D):

  for j in range(len(distances[i])):

    distances2[i]+=[distances2[i][-1]+distances[i][j]]

Q = int(eval(input()))

for i in range(Q):

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

  q = L%D

  l, r = (L-1)//D, (R-1)//D

  print((distances2[q][r]-distances2[q][l]))
H, W, D = list(map(int,input().split()))

dist = [0]*(H*W+1)#dist[i]はiからi+Dまでの距離

pos = [[0,0] for i in range(H*W+1)]

for i in range(H):

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

  for j in range(W):

    pos[A[j]] = [i+1, j+1]

    

for i in range(H*W+1-D):

  dist[i] = abs(pos[i][0]-pos[i+D][0])+abs(pos[i][1]-pos[i+D][1])



distances = [[] for i in range(D)]#あまりがiの距離

for i in range(1,H*W+1-D):

  distances[i%D]+=[dist[i]]

distances2 = [[0] for i in range(D)]#累積和に直す

for i in range(D):

  for j in range(len(distances[i])):

    distances2[i]+=[distances2[i][-1]+distances[i][j]]



Q = int(eval(input()))

for i in range(Q):

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

  l, r = (L-1)//D, (R-1)//D

  print((distances2[L%D][r]-distances2[L%D][l]))
H, W, D = list(map(int, input().split()))
dist = [0] * (H * W + 1)  # dist[i]はiからi+Dまでの距離
pos = [[0, 0] for i in range(H * W + 1)]
for i in range(H):
    A = list(map(int, input().split()))
    for j in range(W):
        pos[A[j]] = [i + 1, j + 1]
for i in range(H * W + 1 - D):
    dist[i] = abs(pos[i][0] - pos[i + D][0]) + abs(pos[i][1] - pos[i + D][1])
distances = [[] for i in range(D)]  # あまりがiの距離
for i in range(1, H * W + 1 - D):
    d = dist[i]
    distances[i % D] += [d]
distances2 = [[0] for i in range(D)]  # 累積和に直す
for i in range(D):
    for j in range(len(distances[i])):
        distances2[i] += [distances2[i][-1] + distances[i][j]]
Q = int(eval(input()))
for i in range(Q):
    L, R = list(map(int, input().split()))
    q = L % D
    l, r = (L - 1) // D, (R - 1) // D
    print((distances2[q][r] - distances2[q][l]))

H, W, D = list(map(int, input().split()))
dist = [0] * (H * W + 1)  # dist[i]はiからi+Dまでの距離
pos = [[0, 0] for i in range(H * W + 1)]
for i in range(H):
    A = list(map(int, input().split()))
    for j in range(W):
        pos[A[j]] = [i + 1, j + 1]
for i in range(H * W + 1 - D):
    dist[i] = abs(pos[i][0] - pos[i + D][0]) + abs(pos[i][1] - pos[i + D][1])
distances = [[] for i in range(D)]  # あまりがiの距離
for i in range(1, H * W + 1 - D):
    distances[i % D] += [dist[i]]
distances2 = [[0] for i in range(D)]  # 累積和に直す
for i in range(D):
    for j in range(len(distances[i])):
        distances2[i] += [distances2[i][-1] + distances[i][j]]
Q = int(eval(input()))
for i in range(Q):
    L, R = list(map(int, input().split()))
    l, r = (L - 1) // D, (R - 1) // D
    print((distances2[L % D][r] - distances2[L % D][l]))

-    d = dist[i] -    distances[i % D] += [d] +    distances[i % D] += [dist[i]] -    q = L % D -    print((distances2[q][r] - distances2[q][l])) +    print((distances2[L % D][r] - distances2[L % D][l]))