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

  println(((1 to reader.nextInt).map{i =>
    val l, r = reader.nextInt
    ((l to r by d).sliding(2).map{l =>
      val lPos = table(l.head)
      val rPos = table(l.last)
      Math.abs(lPos._1 - rPos._1) + Math.abs(lPos._2 - rPos._2)
    }).sum
  }).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
  }
}
}