import scala.io.{StdIn => in}

object Main extends App {
  val Array(h, w, d) = in.readLine.split(" ").map(_.toInt)
  case class Point(x:Int, y:Int) {
    def -(that: Point): Point = new Point(x - that.x, y - that.y)
    def mDist(): Int = this.x.abs + this.y.abs
  }
  val table = Array.ofDim[Point](h*w+1)
  val grid = Iterator.continually(in.readLine)
    .take(h)
    .map(_.split(" ").map(_.toInt))
    .toArray
  
  for{
    i <- 0 until h
    j <- 0 until w
  } table(grid(i)(j)) = new Point(y = i, x = j)


  val res = Array.ofDim[Int](h*w+1)
  for(i <- 1 to h*w-d) {
    res(i+d) = res(i) + (table(i+d) - table(i)).mDist 
  }

  val out = new java.io.PrintWriter(System.out)
  val q = in.readInt
  Iterator.continually(in.readLine)
    .take(q)
    .map(_.split(" ")map(_.toInt))
    .foreach (e => {
      val l = e(0)
      var r = e(1)
      out.println(res(r) - res(l))
    })
  out.flush
}