#include<iostream>
using namespace std;

int main(void){
	int H,W,D;
	cin>>H>>W>>D;
	int x[H*W+1],y[H*W+1],dist[H*W+1];
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
        	int temp;
        	cin>>temp;
			x[temp] = i+1;
			y[temp] = j+1;
        }
    }
    for(int i=1;i<D+1;i++)dist[i] = 0;
    for(int i=D+1;i<H*W+1;i++){
		dist[i] = dist[i-D]+abs(x[i]-x[i-D])+abs(y[i]-y[i-D]);
    }
  	int Q;
    cin>>Q;
	for(int i=0;i<Q;i++){
		int L,R;
		cin>>L>>R;
		cout<<dist[R]-dist[L]<<endl;
    }
  return 0;
}
