# coding:utf-8



import sys



input = sys.stdin.readline

INF = float('inf')

MOD = 10 ** 9 + 7





def inpl(): return list(map(int, input().split()))





H, W, D = inpl()



num_coordinate = {}

for i in range(H):

    w = inpl()

    for j in range(W):

        num_coordinate[w[j]] = (i, j)



c_sum = {}

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

    c_sum[d] = [0]



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

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

        c_sum[d].append(c_sum[d][-1] + abs(num_coordinate[n][0] - num_coordinate[n - D][0])

                        + abs(num_coordinate[n][1] - num_coordinate[n - D][1]))



Q = int(eval(input()))

for _ in range(Q):

    l, r = inpl()

    index = (l - 1) % D + 1

    index_l, index_r = (l - 1) // D, (r - 1) // D



    print((c_sum[index][index_r] - c_sum[index][index_l]))

# coding:utf-8



import sys



INF = float('inf')

MOD = 10 ** 9 + 7



def LI(): return [int(x) for x in sys.stdin.readline().split()]

def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]

def LS(): return sys.stdin.readline().split()

def II(): return int(sys.stdin.readline())

def SI(): return eval(input())





h, w, d = LI()

coord = [0] * (h * w)

for y in range(h):

    for x, n in enumerate(LI_()):

        coord[n] = (y, x)



C = [[0] for _ in range(d)]

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

    diff = abs(coord[i][0] - coord[i - d][0]) + abs(coord[i][1] - coord[i - d][1])

    C[i % d].append(C[i % d][-1] + diff)



q = II()

for _ in range(q):

    l, r = LI_()

    n = l % d

    print((C[n][r // d] - C[n][l // d]))

# coding:utf-8
import sys

input = sys.stdin.readline
INF = float("inf")
MOD = 10**9 + 7


def inpl():
    return list(map(int, input().split()))


H, W, D = inpl()
num_coordinate = {}
for i in range(H):
    w = inpl()
    for j in range(W):
        num_coordinate[w[j]] = (i, j)
c_sum = {}
for d in range(1, D + 1):
    c_sum[d] = [0]
for d in range(1, D + 1):
    for n in range(d + D, H * W + 1, D):
        c_sum[d].append(
            c_sum[d][-1]
            + abs(num_coordinate[n][0] - num_coordinate[n - D][0])
            + abs(num_coordinate[n][1] - num_coordinate[n - D][1])
        )
Q = int(eval(input()))
for _ in range(Q):
    l, r = inpl()
    index = (l - 1) % D + 1
    index_l, index_r = (l - 1) // D, (r - 1) // D
    print((c_sum[index][index_r] - c_sum[index][index_l]))

# coding:utf-8
import sys

INF = float("inf")
MOD = 10**9 + 7


def LI():
    return [int(x) for x in sys.stdin.readline().split()]


def LI_():
    return [int(x) - 1 for x in sys.stdin.readline().split()]


def LS():
    return sys.stdin.readline().split()


def II():
    return int(sys.stdin.readline())


def SI():
    return eval(input())


h, w, d = LI()
coord = [0] * (h * w)
for y in range(h):
    for x, n in enumerate(LI_()):
        coord[n] = (y, x)
C = [[0] for _ in range(d)]
for i in range(d, h * w):
    diff = abs(coord[i][0] - coord[i - d][0]) + abs(coord[i][1] - coord[i - d][1])
    C[i % d].append(C[i % d][-1] + diff)
q = II()
for _ in range(q):
    l, r = LI_()
    n = l % d
    print((C[n][r // d] - C[n][l // d]))

-input = sys.stdin.readline -def inpl(): -    return list(map(int, input().split())) +def LI(): +    return [int(x) for x in sys.stdin.readline().split()] -H, W, D = inpl() -num_coordinate = {} -for i in range(H): -    w = inpl() -    for j in range(W): -        num_coordinate[w[j]] = (i, j) -c_sum = {} -for d in range(1, D + 1): -    c_sum[d] = [0] -for d in range(1, D + 1): -    for n in range(d + D, H * W + 1, D): -        c_sum[d].append( -            c_sum[d][-1] -            + abs(num_coordinate[n][0] - num_coordinate[n - D][0]) -            + abs(num_coordinate[n][1] - num_coordinate[n - D][1]) -        ) -Q = int(eval(input())) -for _ in range(Q): -    l, r = inpl() -    index = (l - 1) % D + 1 -    index_l, index_r = (l - 1) // D, (r - 1) // D -    print((c_sum[index][index_r] - c_sum[index][index_l])) +def LI_(): +    return [int(x) - 1 for x in sys.stdin.readline().split()] + + +def LS(): +    return sys.stdin.readline().split() + + +def II(): +    return int(sys.stdin.readline()) + + +def SI(): +    return eval(input()) + + +h, w, d = LI() +coord = [0] * (h * w) +for y in range(h): +    for x, n in enumerate(LI_()): +        coord[n] = (y, x) +C = [[0] for _ in range(d)] +for i in range(d, h * w): +    diff = abs(coord[i][0] - coord[i - d][0]) + abs(coord[i][1] - coord[i - d][1]) +    C[i % d].append(C[i % d][-1] + diff) +q = II() +for _ in range(q): +    l, r = LI_() +    n = l % d +    print((C[n][r // d] - C[n][l // d]))