object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val h, w, d = sc.nextInt()
  val n = h * w;
  val pos = Array.fill(n)((0, 0))
  for {
    i <- 0 until h
    j <- 0 until w
  } {
    val a = sc.nextInt() - 1
    pos(a) = (i, j)
  }
  val c = Array.fill(d, n / d)(0)
  for {
    i <- 0 until d
    j <- 0 until n / d
  } {
    if (i + (j + 1) * d < n) {
      val (x1, y1) = pos(i + j * d)
      val (x2, y2) = pos(i + (j + 1) * d)
      c(i)(j) = (x1 - x2).abs + (y1 - y2).abs
    }
  }

  val ca = c.map(_.scanLeft(0)(_ + _))

  val q = sc.nextInt()
  val lr = List.fill(q)((sc.nextInt()-1, sc.nextInt()-1))
  val pw = new java.io.PrintWriter(Console.out)
  for ((l, r) <- lr) {
    val x = l % d
    pw.println(ca(x)(r / d) - ca(x)(l / d))
  }
  pw.flush()
}
