void main(){
  import std.stdio, std.string, std.conv, std.algorithm;
  import std.typecons, std.math;

  int h, w, d; rd(h, w, d);
  auto c=new int[][](h, w);
  foreach(i; 0..h) c[i]=readln.split.to!(int[]);
  foreach(i; 0..h)foreach(j; 0..w) c[i][j]--;

  alias P=Tuple!(int, "i", int, "j");
  auto map=new P[](h*w);
  foreach(int i; 0..h)foreach(int j; 0..w) map[c[i][j]]=P(i, j);

  long cost(P a, P b){
    return (a.i-b.i).abs+(a.j-b.j).abs;
  }
  auto dp=new long[](h*w);
  foreach(i; d..(h*w)) dp[i]=dp[i-d]+cost(map[i-d], map[i]);
  int q; rd(q);
  while(q--){
    int l, r; rd(l, r); l--; r--;
    writeln(dp[r]-dp[l]);
  }
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}