open Scanf
let rec for_iter_ a b f = if a >= b then () else (f a; for_iter_ (a+1) b f)
let () =
  let h, w, d = scanf "%d %d %d" (fun a b c -> a, b, c) in
  let pos = Array.make (h*w+1) (-1, -1) in
  let steps = Array.make (h*w+1) (h*w) in

  for_iter_ 0 h (fun i -> for_iter_ 0 w (fun j -> scanf " %d" (fun v -> pos.(v) <- (i, j))));

  for_iter_ 0 d (fun i -> steps.(i) <- 0);
  for_iter_ d (h*w+1) (fun i ->
    let (x', y'), (x, y) = pos.(i-d), pos.(i) in
    steps.(i) <- steps.(i-d) + abs (x-x') + abs (y-y'));

  scanf " %d" (fun q -> for_iter_ 0 q (fun _ ->
    scanf " %d %d" (fun x y -> steps.(y) - steps.(x))
    |> Printf.printf "%d\n"))