#include<iostream>
#include <complex>
using namespace std;
int main(){
  int H,W,D,N;
  cin >>H >>W >>D;
  N=H*W;
  int x[N];
  int y[N];
  int goal[N];
  int i,j;
  int now;
  for(i=0;i<H;i++){
    for(j=0;j<W;j++){
      cin >> now;
      x[now-1]=i;
      y[now-1]=j;
    }
  }
  for(i=0;i<N;i++){
    if (i<D){
      goal[i]=0;
    }else{
      goal[i]=goal[i-D]+abs(x[i]-x[i-D])+abs(y[i]-y[i-D]);
    }
  }
  int Q,L,R;
  cin >> Q;
  for(i=0;i<Q;i++){
    cin >> L >>R;
    cout << goal[R-1]-goal[L-1] <<"\n";
  }
}
