#include <iostream>

# define REP(i,n) for(int i=0; i<n; i++)

using namespace std;

int main(){
	int h,w,d;
	cin >> h >> w >> d;
	
	int a[90005][2];
	int input;
	
	REP(i,h){
		REP(k,w){
			cin >> input;
			//cout << input << endl;
			a[input-1][0] = i;
			a[input-1][1] = k;
		}
	}
	
	int q;
	cin >> q;
	
	int l,r;
	long long int m[90005];
	REP(i,h*w){
		m[i] = 0;
	}
	
	for (int i=d; i<h*w; i++){
		m[i] = m[i-d] + abs(a[i][0]-a[i-d][0]) + abs(a[i][1]-a[i-d][1]);
	}
		
	
	REP(i,q){
		cin >> l >> r;
		cout << m[r-1]-m[l-1] << endl;
	}
}

