#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main()
{
	int H, W, D;
	cin >> H >> W >> D;
	vector<vector<int>> A(H, vector<int>(W));
	vector<int> x(H * W), y(H * W);
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> A[i][j]; A[i][j]--;
			x[A[i][j]] = i;
			y[A[i][j]] = j;
		}
	}
	vector<ll> sum(H * W);
	for (int i = D; i < H * W; i++) {
		sum[i] = sum[i - D] + (abs(x[i] - x[i - D]) + abs(y[i] - y[i - D]));
	}
	int Q;
	cin >> Q;
	while (Q--) {
		int L, R;
		cin >> L >> R; L--, R--;
		cout << (sum[R] - sum[L]) << endl;
	}
	return 0;
}
