import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}
import Math.abs

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val H, W, D = in.next().toInt
  val A = Array.ofDim[Int](H+1, W+1)
  for(y <- 1 to H; x <- 1 to W) {
    A(y)(x) = in.next().toInt
  }

  val zahyo = new Array[(Int, Int)](H*W+1)
  for(y <- 1 to H; x <- 1 to W) {
    zahyo(A(y)(x)) = (x, y)
  }

  val dp = new Array[Int](H*W+1)

  for(i <- 1 to D) {
    val (ix, iy) = zahyo(i)
    dp(i) = 0

    var cnt = i + D
    var now = 0

    while(cnt <= W*H) {

      val (px, py) = zahyo(cnt - D)
      val (x, y) = zahyo(cnt)

      now += abs(x-px) + abs(y - py)
      dp(cnt) = now

      cnt += D
    }
  }

  val Q = in.next().toInt
  for(_ <- 1 to Q){
    val L, R = in.next().toInt
    val ans = dp(R) - dp(L)
    pw.println(ans.toString)
  }
  pw.flush()
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}
