// D - Practical Skill Test
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
#define rp(i,n) for(int i=0;i<(n);++i)

int main(){
	int H,W,D; cin>>H>>W>>D;

	int MAX = H * W;
	vi HH(MAX+1), WW(MAX+1), DD(MAX+1);

	rp(h, H){
		rp(w, W){
		int a; cin>>a;
		HH[a] = h+1; WW[a] = w+1;
		}
	}

	for(int pre=1; pre<MAX; ++pre){
		int now = pre + D;
		if(now > MAX) break;
		DD[now] = DD[pre]
			+ abs(HH[pre] - HH[now])
			+ abs(WW[pre] - WW[now]);
	}

	int Q; cin>>Q;
	while(Q--){
		int l,r; cin>>l>>r;
		cout<< DD[r] - DD[l] <<endl;
	}
}