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

A = []



for _ in range(h):

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

    A.append(line)



num_to_xy = [[0, 0] for i in range(h*w + 1)]



for y in range(h):

    for x in range(w):

        num = A[y][x]

        num_to_xy[num] = [x, y]



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



for start in range(1, d+1):

    cost = 0

    num = start

    sx, sy = num_to_xy[num]

    while num <= h*w:

        nx, ny = num_to_xy[num]

        cost += abs(nx-sx) + abs(ny-sy)

        costs[num] = cost

        sx, sy = nx, ny

        num += d



q = int(eval(input()))



for _ in range(q):

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

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

input = sys.stdin.readline





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

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

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



for y in range(h):

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

    for x, num in enumerate(line):

        num_to_x[num] = x

        num_to_y[num] = y



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



for start in range(1, d+1):

    cost = 0

    num = start

    sx, sy = num_to_x[num], num_to_y[num]

    while num <= h*w:

        nx, ny = num_to_x[num], num_to_y[num]

        cost += abs(nx-sx) + abs(ny-sy)

        costs[num] = cost

        sx, sy = nx, ny

        num += d



q = int(eval(input()))



for _ in range(q):

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

    print((costs[r] - costs[l]))
h, w, d = list(map(int, input().split()))
A = []
for _ in range(h):
    line = list(map(int, input().split()))
    A.append(line)
num_to_xy = [[0, 0] for i in range(h * w + 1)]
for y in range(h):
    for x in range(w):
        num = A[y][x]
        num_to_xy[num] = [x, y]
costs = [0] * (h * w + 1)
for start in range(1, d + 1):
    cost = 0
    num = start
    sx, sy = num_to_xy[num]
    while num <= h * w:
        nx, ny = num_to_xy[num]
        cost += abs(nx - sx) + abs(ny - sy)
        costs[num] = cost
        sx, sy = nx, ny
        num += d
q = int(eval(input()))
for _ in range(q):
    l, r = list(map(int, input().split()))
    print((costs[r] - costs[l]))

import sys

input = sys.stdin.readline
h, w, d = list(map(int, input().split()))
num_to_x = [0] * (h * w + 1)
num_to_y = [0] * (h * w + 1)
for y in range(h):
    line = list(map(int, input().split()))
    for x, num in enumerate(line):
        num_to_x[num] = x
        num_to_y[num] = y
costs = [0] * (h * w + 1)
for start in range(1, d + 1):
    cost = 0
    num = start
    sx, sy = num_to_x[num], num_to_y[num]
    while num <= h * w:
        nx, ny = num_to_x[num], num_to_y[num]
        cost += abs(nx - sx) + abs(ny - sy)
        costs[num] = cost
        sx, sy = nx, ny
        num += d
q = int(eval(input()))
for _ in range(q):
    l, r = list(map(int, input().split()))
    print((costs[r] - costs[l]))

+import sys + +input = sys.stdin.readline -A = [] -for _ in range(h): +num_to_x = [0] * (h * w + 1) +num_to_y = [0] * (h * w + 1) +for y in range(h): -    A.append(line) -num_to_xy = [[0, 0] for i in range(h * w + 1)] -for y in range(h): -    for x in range(w): -        num = A[y][x] -        num_to_xy[num] = [x, y] +    for x, num in enumerate(line): +        num_to_x[num] = x +        num_to_y[num] = y -    sx, sy = num_to_xy[num] +    sx, sy = num_to_x[num], num_to_y[num] -        nx, ny = num_to_xy[num] +        nx, ny = num_to_x[num], num_to_y[num]