#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;


int main(){
	ll h,w,d;
	cin >> h >> w >> d;
	
	vector<P> loc(h*w);
	rep(i,h){
		rep(j,w){
			int tmp;
			cin >> tmp;
			loc[--tmp] = make_pair(i,j);
		}
	}
	
	vector<ll> s(h*w);
	rep(i,h*w){
		if(i-d >= 0) s[i] = s[i-d] + abs(loc[i-d].first - loc[i].first) + abs(loc[i-d].second - loc[i].second);
	}
	
	ll q;
	cin >> q;
	rep(i,q){
		ll l,r;
		cin >> l >> r;
		cout << s[--r]-s[--l] << endl;
	}

	return 0;
}