object Main extends App {
  val sr = new StdinReader
  val h, w, d = sr.nextInt
  val table: Map[Int, (Int, Int)] = (for {
        y <- 0 until h
        x <- 0 until w
      } yield sr.nextInt -> (x, y)).toMap

  val tbl = new Array[Int](h * w + 1)
  (1 to d).flatMap{dd =>
    ((dd + d) to (h * w) by d).map {p => 
      val lPos = table(p - d)
      val rPos = table(p)
      val mp = Math.abs(lPos._1 - rPos._1) + Math.abs(lPos._2 - rPos._2)
      tbl(p) = mp + tbl(p - d)
    }
  }

  println(
    (1 to sr.nextInt).map{i =>
      val l, r = sr.nextInt
      tbl(r) - tbl(l)
    }.mkString("\n"))


class StdinReader {
  val is = System.in
  val buffer = new Array[Byte](1024 * 1024)
  var size = 0
  var pos = 0

  private def readByte: Byte = {
    if (pos >= size) {
      pos = 0;
      size = is.read(buffer);
    }
    if (size < 0) -1
    else {
      val ret = buffer(pos)
      pos += 1
      ret
    }
  }

  // support single byte char only
  def next: String = {
    val sb = new StringBuilder
    var reading = false
    var completed = false
    while(!completed) {
      val b = readByte

      if ('!' <= b && b <= '~') {
        sb.append(b.asInstanceOf[Char])
        reading = true
      } else {
        if (reading) completed = true
      }
    }
    sb.toString
  }

  def nextInt: Int = {
    var i = 0
    var reading = false
    var completed = false
    while(!completed) {
      val b = readByte

      if (b == '-') {
        reading = true
        i *= -1
      } else if ('0' <= b && b <= '9') {
        reading = true
        i = i * 10 + (b - '0')
      } else if (reading) completed = true
    }
    i
  }
}
}