import java.util.*

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)
    val h = sc.nextInt()
    val w = sc.nextInt()
    val d = sc.nextInt()
    val t = mutableListOf<Triple<Int, Int, Int>>()
    val a = (1..h).map { i ->
        (1..w).map { j ->
            val x = sc.nextInt()
            t.add(Triple(x, i, j))
            x
        }
    }
    t.sortBy { it.first }

    val sum = Array(h * w) { 0L }
    for (from in t) {
        val fromValue = from.first
        val fromI = from.second
        val fromJ = from.third

        val toValue = fromValue + d
        if (toValue <= h * w) {
            val to = t[toValue - 1]
            val toI = to.second
            val toJ = to.third
            sum[toValue - 1] = sum[fromValue - 1] + Math.abs(fromI - toI) + Math.abs(fromJ - toJ)
        }
    }

    val q = sc.nextInt()
    repeat(q) {
        val l = sc.nextInt() - 1
        val r = sc.nextInt() - 1
        println(sum[r] - sum[l])
    }
}
