local mfl, mab = math.floor, math.abs
local h, w, d = io.read("*n", "*n", "*n")
local n = h * w
local map = {}
local cost = {}
for i = 1, n do
  map[i] = io.read("*n")
  cost[i] = 0
end
for t_i_d = 1, d do
  local src_x, src_y = 0, 0
  local costsum = 0
  local i_d = t_i_d
  while i_d <= n do
    for i = 1, n do
      if map[i] == i_d then
        local dst_x, dst_y = 1 + (i - 1) % w, 1 + mfl((i - 1) / w)
        costsum = costsum + mab(dst_x - src_x) + mab(dst_y - src_y)
        cost[i_d] = costsum
        src_x, src_y = dst_x, dst_y
        break
      end
    end
    i_d = i_d + d
  end
end
local q = io.read("*n")
for i = 1, q do
  local l, r = io.read("*n", "*n")
  print(cost[r] - cost[l])
end
