function Main(input) {
	input = input.split("\n")
	var input0 = input[0].split(" ")
	var Height = parseInt(input0[0]),
		Width = parseInt(input0[1]),
		D = parseInt(input0[2])
	var board = input.slice(1, Height+1).map(x => x.split(" ").map(y => parseInt(y)))
	var Quest = parseInt(input[Height + 1])
	var LR = input.slice(Height + 2).map(x => x.split(" ").map(y => parseInt(y)))
	var searchValueFromBoard = function(value) {
		for(var i = 0; i < Height; i++) {
			for(var j = 0; j < Width; j++) {
				if(board[i][j] == value) return [i, j]
			}
		}
	}

	var results = []
	var knownResults = {}
	for(var q = 0; q < Quest; q++) {
		if(knownResults[LR[q]] != undefined) {
			results.push(knownResults[LR[q]])
		} else {
			var firstV = LR[q][0]
			var lastV= LR[q][1]

			var magicPoints = 0
			//first, ... //// -last-
			for(var currentV = firstV; currentV < lastV; currentV += 2){
				var nextV = currentV + D
				var current = searchValueFromBoard(currentV)
				var next = searchValueFromBoard(nextV)
				magicPoints += Math.abs(next[0] - current[0]) + Math.abs(next[1] - current[1])
			}

			knownResults[LR[q]] = magicPoints

			results.push(magicPoints)
		}
	}

	console.log(results.join("\n"))

}

//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));