import sys

sdin = sys.stdin.readline



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

A = []

for h in range(H):

    A.append(list(map(int, sdin().split())))

Q = int(sdin())

RL = []

for q in range(Q):

    RL.append(tuple(map(int, sdin().split())))





codn = {}

for h in range(H):

    for w in range(W):

        codn[A[h][w]] = (h+1, w+1)

        

# mod D上での累積和

cums = []

for d in range(D):

    cum = []

    res = D

    if d!=0:

        res = d

    

    cum = [0]

    while res+D <= H*W:

        prev = codn[res]

        nex = codn[res+D]

        cost = abs(nex[0]-prev[0]) + abs(nex[1]-prev[1])

        cum.append(cum[-1]+cost)

        

        res += D

    

    cums.append(cum)

    

# 各クエリの処理

ans = 0

for query in range(Q):

    q = RL[query]

    mod = q[0] % D

    start = (q[0]-mod) // D

    last = (q[1]-mod) // D

    if mod == 0:

        start -= 1

        last -= 1

    

    ans = cums[mod][last] - cums[mod][start]   

    

    print(ans)
import sys

stdin = sys.stdin

 

sys.setrecursionlimit(10**5) 

 

def li(): return list(map(int, stdin.readline().split()))

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

def lf(): return list(map(float, stdin.readline().split()))

def ls(): return stdin.readline().split()

def ns(): return stdin.readline().rstrip()

def lc(): return list(ns())

def ni(): return int(stdin.readline())

def nf(): return float(stdin.readline())



# パスを計算

h,w,d = li()

grid = []

dic = {}

for i in range(h):

    row = list(li())

    for j in range(w):

        dic.update({row[j]: (i,j)})



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



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

    dist[i] = dist[i-d] + abs(dic[i][0] - dic[i-d][0]) + abs(dic[i][1] - dic[i-d][1])

    

q = ni()

for _ in range(q):

    l,r = li()

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

sdin = sys.stdin.readline
H, W, D = list(map(int, sdin().split()))
A = []
for h in range(H):
    A.append(list(map(int, sdin().split())))
Q = int(sdin())
RL = []
for q in range(Q):
    RL.append(tuple(map(int, sdin().split())))
codn = {}
for h in range(H):
    for w in range(W):
        codn[A[h][w]] = (h + 1, w + 1)
# mod D上での累積和
cums = []
for d in range(D):
    cum = []
    res = D
    if d != 0:
        res = d
    cum = [0]
    while res + D <= H * W:
        prev = codn[res]
        nex = codn[res + D]
        cost = abs(nex[0] - prev[0]) + abs(nex[1] - prev[1])
        cum.append(cum[-1] + cost)
        res += D
    cums.append(cum)
# 各クエリの処理
ans = 0
for query in range(Q):
    q = RL[query]
    mod = q[0] % D
    start = (q[0] - mod) // D
    last = (q[1] - mod) // D
    if mod == 0:
        start -= 1
        last -= 1
    ans = cums[mod][last] - cums[mod][start]
    print(ans)

import sys

stdin = sys.stdin
sys.setrecursionlimit(10**5)


def li():
    return list(map(int, stdin.readline().split()))


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


def lf():
    return list(map(float, stdin.readline().split()))


def ls():
    return stdin.readline().split()


def ns():
    return stdin.readline().rstrip()


def lc():
    return list(ns())


def ni():
    return int(stdin.readline())


def nf():
    return float(stdin.readline())


# パスを計算
h, w, d = li()
grid = []
dic = {}
for i in range(h):
    row = list(li())
    for j in range(w):
        dic.update({row[j]: (i, j)})
dist = [0] * (h * w + 1)
for i in range(d + 1, h * w + 1):
    dist[i] = (
        dist[i - d] + abs(dic[i][0] - dic[i - d][0]) + abs(dic[i][1] - dic[i - d][1])
    )
q = ni()
for _ in range(q):
    l, r = li()
    print((dist[r] - dist[l]))

-sdin = sys.stdin.readline -H, W, D = list(map(int, sdin().split())) -A = [] -for h in range(H): -    A.append(list(map(int, sdin().split()))) -Q = int(sdin()) -RL = [] -for q in range(Q): -    RL.append(tuple(map(int, sdin().split()))) -codn = {} -for h in range(H): -    for w in range(W): -        codn[A[h][w]] = (h + 1, w + 1) -# mod D上での累積和 -cums = [] -for d in range(D): -    cum = [] -    res = D -    if d != 0: -        res = d -    cum = [0] -    while res + D <= H * W: -        prev = codn[res] -        nex = codn[res + D] -        cost = abs(nex[0] - prev[0]) + abs(nex[1] - prev[1]) -        cum.append(cum[-1] + cost) -        res += D -    cums.append(cum) -# 各クエリの処理 -ans = 0 -for query in range(Q): -    q = RL[query] -    mod = q[0] % D -    start = (q[0] - mod) // D -    last = (q[1] - mod) // D -    if mod == 0: -        start -= 1 -        last -= 1 -    ans = cums[mod][last] - cums[mod][start] -    print(ans) +stdin = sys.stdin +sys.setrecursionlimit(10**5) + + +def li(): +    return list(map(int, stdin.readline().split())) + + +def li_(): +    return [int(x) - 1 for x in stdin.readline().split()] + + +def lf(): +    return list(map(float, stdin.readline().split())) + + +def ls(): +    return stdin.readline().split() + + +def ns(): +    return stdin.readline().rstrip() + + +def lc(): +    return list(ns()) + + +def ni(): +    return int(stdin.readline()) + + +def nf(): +    return float(stdin.readline()) + + +# パスを計算 +h, w, d = li() +grid = [] +dic = {} +for i in range(h): +    row = list(li()) +    for j in range(w): +        dic.update({row[j]: (i, j)}) +dist = [0] * (h * w + 1) +for i in range(d + 1, h * w + 1): +    dist[i] = ( +        dist[i - d] + abs(dic[i][0] - dic[i - d][0]) + abs(dic[i][1] - dic[i - d][1]) +    ) +q = ni() +for _ in range(q): +    l, r = li() +    print((dist[r] - dist[l]))