#■標準入力ショートカット


def get_next_int():
    return int(float(input()))


def get_next_ints(delim=" "):
    return tuple([int(float(x)) for x in input().split(delim)])


def get_next_str():
    return input()


def get_next_strs(delim=" "):
    return tuple(input().split(delim))


def get_next_by_types(*value_types, delim=" "):
    return tuple([t(x) for t, x in zip(value_types, input().split(delim))])

class PracticalSkillTest():

    def __init__(self):
        self.H, self.W, self.D = get_next_ints()
        self.field = []
        for _ in range(self.H):
            self.field.append(get_next_ints())
        self.Q = get_next_int()
        self.places = []
        for _ in range(self.Q):
            self.places.append(get_next_ints())
        self.mapping()
        self.dp = [[-1]*(self.H*self.W) for i in range(self.H*self.W)]

    def mapping(self):
        self.mapping_dic = {}
        for y in range(self.H):
            for x in range(self.W):
                self.mapping_dic[self.field[y][x]] = (x, y)


    def get_consumption(self, num):
        start = self.places[num][0]
        end = self.places[num][1]
        if self.dp[start][end] != -1:
            return self.dp[start][end]
        pos = (self.mapping_dic[start])
        cost = 0
        for i in range(start+self.D, end+1, self.D):
            next_pos = self.mapping_dic[i]
            cost += abs(pos[0] - next_pos[0]) + abs(pos[1] - next_pos[1])
            pos = next_pos
        self.dp[start][end] = cost
        return cost


    def practice_solve(self):
        for i in range(self.Q):
            cost = self.get_consumption(i)
            print(cost)
    
def solve():
    p = PracticalSkillTest()
    p.practice_solve()


solve()

### FIXED CODE ###


import numpy as np


def get_next_int():
    return int(float(input()))


def get_next_ints(delim=" "):
    return tuple([int(float(x)) for x in input().split(delim)])


def get_next_str():
    return input()


def get_next_strs(delim=" "):
    return tuple(input().split(delim))


def get_next_by_types(*value_types, delim=" "):
    return tuple([t(x) for t, x in zip(value_types, input().split(delim))])

class PracticalSkillTest():

    def __init__(self):
        self.H, self.W, self.D = get_next_ints()
        self.field = []
        for _ in range(self.H):
            self.field.append(get_next_ints())
        self.Q = get_next_int()
        self.places = []
        for _ in range(self.Q):
            self.places.append(get_next_ints())
        self.mapping()
        self.dp = np.zeros(self.H * self.W+1, dtype=np.int)
        self.calc_cumulative_sum()

    def mapping(self):
        self.mapping_dic = {}
        for y in range(self.H):
            for x in range(self.W):
                self.mapping_dic[self.field[y][x]] = (x, y)


    def calc_cumulative_sum(self):
        for i in range(self.D+1, self.H*self.W+1):
            pos = self.mapping_dic[i]
            pre_pos = self.mapping_dic[i-self.D]
            self.dp[i] = self.dp[i-self.D] + abs(pos[0] - pre_pos[0]) + abs(pos[1] - pre_pos[1])
        # print(self.dp)



    def practice_solve(self):
        self.calc_cumulative_sum()
        for i in range(self.Q):
            cost = self.dp[self.places[i][1]] - self.dp[self.places[i][0]]
            print(cost)
    
def solve():
    p = PracticalSkillTest()
    p.practice_solve()

solve()