#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <map>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;
typedef pair<int,int> ii_t;

int a[300][300] = {0};
int prom[300*300 + 10] = {0};
int diff[300*300 + 10] = {0};

int main() {
    int h, w, d, q, left, right;
    cin >> h >> w >> d;
    map<int,ii_t> pos;
    for (int i=0;i<h;i++)
        for (int j=0;j<w;j++) {
            cin >> a[i][j];

            pos[a[i][j]] = ii_t(i,j);
        }
    int hw = h*w;
    for (int k=1;k<=hw;k++) {
        if (k-d <= 0) {
            prom[k] = 0;
        } else {
            ii_t fr = pos[k-d];
            ii_t to = pos[k];

            prom[k] = abs(to.first-fr.first) + abs(to.second-fr.second);
        }
        if (k-d <= 0) {
            diff[k] = 0;
        } else {
            diff[k] += diff[k-d] + prom[k];
        }
        // cerr << "diff: " << k << " " << diff[k] << endl;
    }

    for (cin >> q ; q -- ; ) {
        cin >> left >> right;
        cout << (diff[right] - diff[left]) << endl;
    }
    return 0;
}
