import std.stdio,
       std.string,
       std.conv,
       std.typecons,
       std.algorithm;

int abs(int n) {
  return n >= 0 ? n : -n;
}

void main() {
  int[] buf = readln.chomp.split.to!(int[]);
  int H = buf[0], W = buf[1], D = buf[2];
  alias Tuple!(int, "x", int, "y") Grid;
  Grid[int] A;

  for (int i = 1; i <= H; i++) {
    int[] m_line = readln.chomp.split.to!(int[]);
    for (int j = 0; j < m_line.length; j++) {
      A[m_line[j]] = Grid(i, j + 1);
    }
  }

  long[300 * 300 + 1] d;

  for (int i = D + 1; i <= H * W; i++) {
    d[i] += d[i - D] + abs(A[i].x - A[i - D].x) + abs(A[i].y - A[i - D].y);
  }

  int Q = readln.chomp.to!(int);
  
  for (int i = 0; i < Q; i++) {
    buf = readln.chomp.split.to!(int[]);
    int L = buf[0], R = buf[1];
    writeln(d[R] - d[L]);
  }
}
