#include <bits/stdc++.h>
using namespace std;
int a[305][305];
struct node {
  int x;
  int y;
} p[90005];
int ans[90005];
int main() {
  int h, w, d, len;
  scanf("%d%d%d", &h, &w, &d);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      scanf("%d", &a[i][j]);
      int n = a[i][j];
      p[n].x = i;
      p[n].y = j;
    }
  int q;
  len = h * w;
  scanf("%d", &q);
  for (int i = 1; i <= d; i++) {
    int l = i, r = len, sum = 0;
    ans[i] = 0;
    while (l <= r - d) {
      sum = sum + abs(p[l].x - p[l + d].x) + abs(p[l].y - p[l + d].y);
      l = l + d;
      ans[l] = sum;
    }
  }
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", ans[r] - ans[l]);
  }
}
