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

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); 
	
	int h, w, d;
	cin >> h >> w >> d;
	vector<int> x(h * w + 1), y(h * w + 1);
	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) {
			int a;
			cin >> a;
			x[a] = i;
			y[a] = j;
		}
	}
	vector<int> a(h * w + 1);
	for(int i = 1; i <= h * w; i++) {
		if(i <= d) continue;
		a[i] = a[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;
		cout << a[r] - a[l] << '\n';
	}
	return 0;
}
