fun main(args: Array<String>) {
    val (H, W, D) = listOfInt()
    val A = Array(H) { listOfInt() }
    val No = Array(H * W + 1) { Pair(0L, 0L) }
    for (h in 0 until H) {
        for (w in 0 until W) {
            No[A[h][w]] = Pair(h.toLong(), w.toLong())
        }
    }
    val C = LongArray(H * W + 1)
    for (n in 1 .. D) {
        for (m in n + D .. H * W step D) {
            C[m] = Math.abs(No[m].first - No[m - D].first) + Math.abs(No[m].second - No[m - D].second)
            C[m] += C[m - D]
        }
    }
    val Q = nextInt()
    val ans = LongArray(Q) {
        val (L, R) = listOfInt()
        C[R] - C[L]
    }
    println(ans.joinToString("\n"))
}
fun next() = readLine()!!
fun nextInt() = Integer.parseInt(next())
fun nextInt(offset: Int = 0) = nextInt() + offset
fun nextLong() = java.lang.Long.parseLong(next())
fun nextLong(offset: Long = 0L) = nextLong() + offset
fun nextDouble() = next().toDouble()
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map { it.toInt() }
fun listOfLong() = listOfString().map { it.toLong() }