#include<stdio.h>
int main(void){
  char *s="python3 -c\""
"h,w,d=map(int,input().split())\n"
"l=[[0,0]]+[[]for _ in range(h*w)]\n"
"for i in range(h):\n"
"    for j,a in enumerate(map(int,input().split())):\n"
"        l[a]=[i,j]\n"
"for a in l[:d]:\n"
"    a.append(0)\n"
"for i,j in zip(l[d:],l):\n"
"    a,b=i\n"
"    x,y,v=j\n"
"    i.append(abs(x-a)+abs(y-b)+v)\n"
"l=[c for a,b,c in l]\n"
"for _ in range(int(input())):\n"
"    a,b=map(int,input().split())\n"
"    print(l[b]-l[a])\n"
"\"";
  system(s);
  return 0;
}            