h, w, d = list(map(int, input().split()))

amd = {}

for i in range(h):

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

  for j, a in enumerate(al):

    amd[a] = (j+1, i+1)



dsum = [0 for _ in range(d+1)]

for i in range(d+1, h*w+1):

  dsum.append(dsum[-d] + abs(amd[i][0]-amd[i-d][0]) + abs(amd[i][1]-amd[i-d][1]))



q = int(eval(input()))

for _ in range(q):

  ans = 0

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

  print((dsum[r] - dsum[l]))
import sys

input = sys.stdin.readline



def main():

  h, w, d = list(map(int, input().split()))

  axy = [None for _ in range(h*w+1)]

  for y in range(h):

    for x, a in enumerate(map(int, input().split())):

      axy[a] = (x, y)



  dlist = [0 for _ in range(h*w+1)]

  for a in range(d+1, h*w+1):

    dlist[a] = dlist[a-d] + abs(axy[a][0] - axy[a-d][0]) + abs(axy[a][1] - axy[a-d][1])



  q = int(eval(input()))

  for _ in range(q):

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

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





if __name__ == '__main__':

  main()

h, w, d = list(map(int, input().split()))
amd = {}
for i in range(h):
    al = list(map(int, input().split()))
    for j, a in enumerate(al):
        amd[a] = (j + 1, i + 1)
dsum = [0 for _ in range(d + 1)]
for i in range(d + 1, h * w + 1):
    dsum.append(
        dsum[-d] + abs(amd[i][0] - amd[i - d][0]) + abs(amd[i][1] - amd[i - d][1])
    )
q = int(eval(input()))
for _ in range(q):
    ans = 0
    l, r = list(map(int, input().split()))
    print((dsum[r] - dsum[l]))

import sys

input = sys.stdin.readline


def main():
    h, w, d = list(map(int, input().split()))
    axy = [None for _ in range(h * w + 1)]
    for y in range(h):
        for x, a in enumerate(map(int, input().split())):
            axy[a] = (x, y)
    dlist = [0 for _ in range(h * w + 1)]
    for a in range(d + 1, h * w + 1):
        dlist[a] = (
            dlist[a - d]
            + abs(axy[a][0] - axy[a - d][0])
            + abs(axy[a][1] - axy[a - d][1])
        )
    q = int(eval(input()))
    for _ in range(q):
        l, r = list(map(int, input().split()))
        print((dlist[r] - dlist[l]))


if __name__ == "__main__":
    main()

-h, w, d = list(map(int, input().split())) -amd = {} -for i in range(h): -    al = list(map(int, input().split())) -    for j, a in enumerate(al): -        amd[a] = (j + 1, i + 1) -dsum = [0 for _ in range(d + 1)] -for i in range(d + 1, h * w + 1): -    dsum.append( -        dsum[-d] + abs(amd[i][0] - amd[i - d][0]) + abs(amd[i][1] - amd[i - d][1]) -    ) -q = int(eval(input())) -for _ in range(q): -    ans = 0 -    l, r = list(map(int, input().split())) -    print((dsum[r] - dsum[l])) +import sys + +input = sys.stdin.readline + + +def main(): +    h, w, d = list(map(int, input().split())) +    axy = [None for _ in range(h * w + 1)] +    for y in range(h): +        for x, a in enumerate(map(int, input().split())): +            axy[a] = (x, y) +    dlist = [0 for _ in range(h * w + 1)] +    for a in range(d + 1, h * w + 1): +        dlist[a] = ( +            dlist[a - d] +            + abs(axy[a][0] - axy[a - d][0]) +            + abs(axy[a][1] - axy[a - d][1]) +        ) +    q = int(eval(input())) +    for _ in range(q): +        l, r = list(map(int, input().split())) +        print((dlist[r] - dlist[l])) + + +if __name__ == "__main__": +    main()