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

a=[list(map(int,input().split())) for i in range(h)]



def find_element(k):

    global h,w,a

    for i in range(h):

        for j in range(w):

            if a[i][j]==k:

                return (i,j)



def make_distance(S,T):

    return abs(S[0]-T[0])+abs(S[1]-T[1])



x1=[]

x2=[]

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

    l=(h*w-i)//d

    x1.append([0]*(l+1))

    x2.append([[0,0]for i in range(l+1)])



for i in range(h):

    for j in range(w):

        p=a[i][j]%d

        if p==0:

            p=d-1

        else:

            p-=1

        l=(a[i][j]-(p+1))//d

        x2[p][l]=[i,j]





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

    l=len(x1[i-1])

    for j in range(l-1):

        x1[i-1][j+1]=x1[i-1][j]+make_distance(x2[i-1][j],x2[i-1][j+1])



q=int(eval(input()))

for i in range(q):

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

    p=l%d

    if p==0:

        v1=l//d-1

        v2=r//d-1

        print((x1[d-1][v2]-x1[d-1][v1]))

    else:

        v1=l//d

        v2=r//d

        print((x1[p-1][v2]-x1[p-1][v1]))

#print(x)

from math import ceil

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

x=ceil((h*w)/d)

num=[[[-1,-1] for j in range(x)] for i in range(d)]#あまりで場合分け(1→0)

mp=[[0]*x for i in range(d)]

a=[list(map(int,input().split())) for i in range(h)]

for i in range(h):

    for j in range(w):

        k,l=a[i][j]%d,a[i][j]//d

        if k==0:

            num[d-1][l-1]=[i,j]

        else:

            num[k-1][l]=[i,j]

for i in range(d):

    for j in range(x):

        if j!=x-1:

            if num[i][j+1]!=[-1,-1]:

                mp[i][j+1]+=(mp[i][j]+abs(num[i][j+1][0]-num[i][j][0])+abs(num[i][j+1][1]-num[i][j][1]))

            else:

                break

q=int(eval(input()))

for i in range(q):

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

    if l%d!=0:

        e,f=l//d,r//d

    else:

        e,f=l//d-1,r//d-1

    g= d-1 if l%d==0 else l%d-1

    if e==0:

        print((mp[g][f]))

    else:

        print((mp[g][f]-mp[g][e]))

h, w, d = list(map(int, input().split()))
a = [list(map(int, input().split())) for i in range(h)]


def find_element(k):
    global h, w, a
    for i in range(h):
        for j in range(w):
            if a[i][j] == k:
                return (i, j)


def make_distance(S, T):
    return abs(S[0] - T[0]) + abs(S[1] - T[1])


x1 = []
x2 = []
for i in range(1, d + 1):
    l = (h * w - i) // d
    x1.append([0] * (l + 1))
    x2.append([[0, 0] for i in range(l + 1)])
for i in range(h):
    for j in range(w):
        p = a[i][j] % d
        if p == 0:
            p = d - 1
        else:
            p -= 1
        l = (a[i][j] - (p + 1)) // d
        x2[p][l] = [i, j]
for i in range(1, d + 1):
    l = len(x1[i - 1])
    for j in range(l - 1):
        x1[i - 1][j + 1] = x1[i - 1][j] + make_distance(x2[i - 1][j], x2[i - 1][j + 1])
q = int(eval(input()))
for i in range(q):
    l, r = list(map(int, input().split()))
    p = l % d
    if p == 0:
        v1 = l // d - 1
        v2 = r // d - 1
        print((x1[d - 1][v2] - x1[d - 1][v1]))
    else:
        v1 = l // d
        v2 = r // d
        print((x1[p - 1][v2] - x1[p - 1][v1]))
# print(x)

from math import ceil

h, w, d = list(map(int, input().split()))
x = ceil((h * w) / d)
num = [[[-1, -1] for j in range(x)] for i in range(d)]  # あまりで場合分け(1→0)
mp = [[0] * x for i in range(d)]
a = [list(map(int, input().split())) for i in range(h)]
for i in range(h):
    for j in range(w):
        k, l = a[i][j] % d, a[i][j] // d
        if k == 0:
            num[d - 1][l - 1] = [i, j]
        else:
            num[k - 1][l] = [i, j]
for i in range(d):
    for j in range(x):
        if j != x - 1:
            if num[i][j + 1] != [-1, -1]:
                mp[i][j + 1] += (
                    mp[i][j]
                    + abs(num[i][j + 1][0] - num[i][j][0])
                    + abs(num[i][j + 1][1] - num[i][j][1])
                )
            else:
                break
q = int(eval(input()))
for i in range(q):
    l, r = list(map(int, input().split()))
    if l % d != 0:
        e, f = l // d, r // d
    else:
        e, f = l // d - 1, r // d - 1
    g = d - 1 if l % d == 0 else l % d - 1
    if e == 0:
        print((mp[g][f]))
    else:
        print((mp[g][f] - mp[g][e]))

+from math import ceil + +x = ceil((h * w) / d) +num = [[[-1, -1] for j in range(x)] for i in range(d)]  # あまりで場合分け(1→0) +mp = [[0] * x for i in range(d)] - - -def find_element(k): -    global h, w, a -    for i in range(h): -        for j in range(w): -            if a[i][j] == k: -                return (i, j) - - -def make_distance(S, T): -    return abs(S[0] - T[0]) + abs(S[1] - T[1]) - - -x1 = [] -x2 = [] -for i in range(1, d + 1): -    l = (h * w - i) // d -    x1.append([0] * (l + 1)) -    x2.append([[0, 0] for i in range(l + 1)]) -        p = a[i][j] % d -        if p == 0: -            p = d - 1 +        k, l = a[i][j] % d, a[i][j] // d +        if k == 0: +            num[d - 1][l - 1] = [i, j] -            p -= 1 -        l = (a[i][j] - (p + 1)) // d -        x2[p][l] = [i, j] -for i in range(1, d + 1): -    l = len(x1[i - 1]) -    for j in range(l - 1): -        x1[i - 1][j + 1] = x1[i - 1][j] + make_distance(x2[i - 1][j], x2[i - 1][j + 1]) +            num[k - 1][l] = [i, j] +for i in range(d): +    for j in range(x): +        if j != x - 1: +            if num[i][j + 1] != [-1, -1]: +                mp[i][j + 1] += ( +                    mp[i][j] +                    + abs(num[i][j + 1][0] - num[i][j][0]) +                    + abs(num[i][j + 1][1] - num[i][j][1]) +                ) +            else: +                break -    p = l % d -    if p == 0: -        v1 = l // d - 1 -        v2 = r // d - 1 -        print((x1[d - 1][v2] - x1[d - 1][v1])) +    if l % d != 0: +        e, f = l // d, r // d -        v1 = l // d -        v2 = r // d -        print((x1[p - 1][v2] - x1[p - 1][v1])) -# print(x) +        e, f = l // d - 1, r // d - 1 +    g = d - 1 if l % d == 0 else l % d - 1 +    if e == 0: +        print((mp[g][f])) +    else: +        print((mp[g][f] - mp[g][e]))