local H, W, D = io.read("*n", "*n", "*n")
local AH = {}
local AW = {}
for h=1,H do
  for w=1,W do
    local a = io.read("*n")
    AH[a] = h
    AW[a] = w
  end
end
local Q = io.read("*n")
for q=1,Q do
  local l, r = io.read("*n", "*n")
  local cost = 0
  for x=l,r-D,D do
    cost = cost + math.abs(AH[x] - AH[x+D]) + math.abs(AW[x] - AW[x+D])
  end
  print(cost)
end
