#include <bits/stdc++.h>

using namespace std;

pair <int, int> pos[100001];
int cost[100001];
int a[501][501];

 main () {
 	int n, m, d;
 	cin >> n >> m >> d;
 	for (int i = 1;i <= n;i ++) {
 		for (int j = 1;j <= m;j ++) {
 			cin >> a[i][j];
 			pos[a[i][j]] = make_pair (i, j);
 		}
 	}
 	for (int i = 1;i <= n * m;i ++) {
		if (i > d) {
			cost[i] = cost[i - d] + abs (pos[i - d].first - pos[i].first) + abs (pos[i - d].second - pos[i].second);
		}
 	}
 	int q;
 	cin >> q;;
 	while (q --) {
 		int l, r;
 		cin >> l >> r;             
 		cout << cost[r] - cost[l] << endl;
 	}
	return 0; 	 
}