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

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

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

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

for i in range(H):

  inf = input().split()

  for j in range(W):

    a = int(inf[j])

    py[a] = i

    px[a] = j

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

  dp[i] = dp[i-D]+abs(px[i]-px[i-D])+abs(py[i]-py[i-D])



Q = int(eval(input()))

for i in range(Q):

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

  print((dp[R]-dp[L]))
H, W, D, *L = list(map(int, open(0).read().split()))

A = L[:W*H]

Q = L[H*W]

inf = []

for l,r in zip(*[iter(L[H*W+1:])]*2):

  inf += [(l,r)]

dic = {A[i*W+j]:(i,j) for i in range(H) for j in range(W)}

ls = [[] for i in range(D)]

for i in range(D):

  s = i if i!=0 else D

  ls[i].append(0)

  m = s

  n = s

  while True:

    n += D

    if H*W<n:

      n = s

    px, py = dic[m]

    x, y = dic[n]

    ls[i].append(ls[i][-1]+abs(px-x)+abs(py-y))

    m = n

    if m==s:

      break

for l, r in inf:

  i = l%D

  if i==0:

    il = (l-D)//D

    ir = (r-D)//D

  else:

    il = (l-i)//D

    ir = (r-i)//D

  if l<r:

    print((ls[i][ir] - ls[i][il]))

  elif l>r:

    print((ls[i][-1]-ls[i][il]+ls[i][ir]))

  else:

    print((0))
H, W, D = list(map(int, input().split()))
px = [0] * (H * W + 1)
py = [0] * (H * W + 1)
dp = [0] * (H * W + 1)
for i in range(H):
    inf = input().split()
    for j in range(W):
        a = int(inf[j])
        py[a] = i
        px[a] = j
for i in range(D + 1, H * W + 1):
    dp[i] = dp[i - D] + abs(px[i] - px[i - D]) + abs(py[i] - py[i - D])
Q = int(eval(input()))
for i in range(Q):
    L, R = list(map(int, input().split()))
    print((dp[R] - dp[L]))

H, W, D, *L = list(map(int, open(0).read().split()))
A = L[: W * H]
Q = L[H * W]
inf = []
for l, r in zip(*[iter(L[H * W + 1 :])] * 2):
    inf += [(l, r)]
dic = {A[i * W + j]: (i, j) for i in range(H) for j in range(W)}
ls = [[] for i in range(D)]
for i in range(D):
    s = i if i != 0 else D
    ls[i].append(0)
    m = s
    n = s
    while True:
        n += D
        if H * W < n:
            n = s
        px, py = dic[m]
        x, y = dic[n]
        ls[i].append(ls[i][-1] + abs(px - x) + abs(py - y))
        m = n
        if m == s:
            break
for l, r in inf:
    i = l % D
    if i == 0:
        il = (l - D) // D
        ir = (r - D) // D
    else:
        il = (l - i) // D
        ir = (r - i) // D
    if l < r:
        print((ls[i][ir] - ls[i][il]))
    elif l > r:
        print((ls[i][-1] - ls[i][il] + ls[i][ir]))
    else:
        print((0))

-H, W, D = list(map(int, input().split())) -px = [0] * (H * W + 1) -py = [0] * (H * W + 1) -dp = [0] * (H * W + 1) -for i in range(H): -    inf = input().split() -    for j in range(W): -        a = int(inf[j]) -        py[a] = i -        px[a] = j -for i in range(D + 1, H * W + 1): -    dp[i] = dp[i - D] + abs(px[i] - px[i - D]) + abs(py[i] - py[i - D]) -Q = int(eval(input())) -for i in range(Q): -    L, R = list(map(int, input().split())) -    print((dp[R] - dp[L])) +H, W, D, *L = list(map(int, open(0).read().split())) +A = L[: W * H] +Q = L[H * W] +inf = [] +for l, r in zip(*[iter(L[H * W + 1 :])] * 2): +    inf += [(l, r)] +dic = {A[i * W + j]: (i, j) for i in range(H) for j in range(W)} +ls = [[] for i in range(D)] +for i in range(D): +    s = i if i != 0 else D +    ls[i].append(0) +    m = s +    n = s +    while True: +        n += D +        if H * W < n: +            n = s +        px, py = dic[m] +        x, y = dic[n] +        ls[i].append(ls[i][-1] + abs(px - x) + abs(py - y)) +        m = n +        if m == s: +            break +for l, r in inf: +    i = l % D +    if i == 0: +        il = (l - D) // D +        ir = (r - D) // D +    else: +        il = (l - i) // D +        ir = (r - i) // D +    if l < r: +        print((ls[i][ir] - ls[i][il])) +    elif l > r: +        print((ls[i][-1] - ls[i][il] + ls[i][ir])) +    else: +        print((0))