from collections import deque

import sys

sys.setrecursionlimit(10**7)

input = sys.stdin.readline



def solve():

  h,w,d  = (int(i) for i in input().split())

  a = deque([])

  for i in range(h):

    tmp = list(int(i) for i in input().split())

    a.append(tmp)



  query = [False]*(h*w)

  distance = [0]*(h*w+1)

  for i in range(h):

    for j in range(w):

      query[a[i][j] - 1] = (i,j)

  q = int(eval(input()))

  

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

    leftx,lefty = query[i-d]

    rightx,righty = query[i]

    distance[i] = distance[i-d]+abs(rightx-leftx)+abs(righty-lefty)

  #print(distance)



  for i in range(q):

    l,r = (int(m) for m in input().split())

    print((distance[r-1]-distance[l-1]))

    

solve()
from collections import deque

import sys

sys.setrecursionlimit(10**7)

input = sys.stdin.readline



def solve():

  h,w,d  = (int(i) for i in input().split())

  a = deque([])

  for i in range(h):

    tmp = list(int(i) for i in input().split())

    a.append(tmp)



  query = [False]*(h*w)

  distance = [0]*(h*w+1)

  for i in range(h):

    for j in range(w):

      query[a[i][j] - 1] = (i,j)

  q = int(eval(input()))

  

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

    leftx,lefty = query[i-d]

    rightx,righty = query[i]

    distance[i] = distance[i-d]+abs(rightx-leftx)+abs(righty-lefty)

  #print(distance)

  ans = []

  for i in range(q):

    l,r = (int(m) for m in input().split())

    ans.append(distance[r-1]-distance[l-1])

  print(("\n".join(map(str,ans))))

solve()
from collections import deque
import sys

sys.setrecursionlimit(10**7)
input = sys.stdin.readline


def solve():
    h, w, d = (int(i) for i in input().split())
    a = deque([])
    for i in range(h):
        tmp = list(int(i) for i in input().split())
        a.append(tmp)
    query = [False] * (h * w)
    distance = [0] * (h * w + 1)
    for i in range(h):
        for j in range(w):
            query[a[i][j] - 1] = (i, j)
    q = int(eval(input()))
    for i in range(d, h * w):
        leftx, lefty = query[i - d]
        rightx, righty = query[i]
        distance[i] = distance[i - d] + abs(rightx - leftx) + abs(righty - lefty)
    # print(distance)
    for i in range(q):
        l, r = (int(m) for m in input().split())
        print((distance[r - 1] - distance[l - 1]))


solve()

from collections import deque
import sys

sys.setrecursionlimit(10**7)
input = sys.stdin.readline


def solve():
    h, w, d = (int(i) for i in input().split())
    a = deque([])
    for i in range(h):
        tmp = list(int(i) for i in input().split())
        a.append(tmp)
    query = [False] * (h * w)
    distance = [0] * (h * w + 1)
    for i in range(h):
        for j in range(w):
            query[a[i][j] - 1] = (i, j)
    q = int(eval(input()))
    for i in range(d, h * w):
        leftx, lefty = query[i - d]
        rightx, righty = query[i]
        distance[i] = distance[i - d] + abs(rightx - leftx) + abs(righty - lefty)
    # print(distance)
    ans = []
    for i in range(q):
        l, r = (int(m) for m in input().split())
        ans.append(distance[r - 1] - distance[l - 1])
    print(("\n".join(map(str, ans))))


solve()

+    ans = [] -        print((distance[r - 1] - distance[l - 1])) +        ans.append(distance[r - 1] - distance[l - 1]) +    print(("\n".join(map(str, ans))))