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);
    }
  }

  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];
    int magical_power;
    while (L != R) {
      magical_power += abs(A[L + D].x - A[L].x) + abs(A[L + D].y - A[L].y);
      L += D;
    }
    writeln(magical_power);
  }
}
