{-# LANGUAGE BangPatterns #-}
import Control.Applicative((<$>))
import Control.Monad
import Data.Map ((!))
import qualified Data.Map as M
import qualified Data.List as L
import Data.Maybe (catMaybes)
-- import Debug.Trace
-- import qualified Data.Char as C
-- import qualified Data.IntSet as I
-- import Text.Regex.Posix ((=~))
--------------------------------------------
readInt    = read :: String -> Int
getInt     = readInt <$> getLine
getIntList = map readInt . words <$> getLine
int2bin 0 =  []
int2bin n =  n `mod` 2 : int2bin (n `div` 2)
----------------------------------------------

data Pos = Pos { x :: Int, y :: Int } deriving (Show)

mkMap [] _ m = m
mkMap (xs:xss) j m = mkMap xss (j+1) (_mkMap xs 1 j m)
  where
    _mkMap [] _ _ m = m
    _mkMap (a:as) i j m = _mkMap as (i+1) j $ (a, Pos i j):m

solve l (Pos lx ly) r d m cost
  | l == r    = cost
  | otherwise =
      let pos = m ! (l+d)
      in solve (l+d) pos r d m (cost + abs(x pos - lx) + abs(y pos - ly))

for [] _ _ _ = return ()
for ([l,r]:lrs) d m cach
  | l == r = do
      print 0
      for lrs d m cach
  | otherwise = do
      let Pos x y = m ! l
          ans = solve l (Pos x y) r d m 0
      print ans
      for lrs d m cach

main :: IO ()
main = do
  [h, w, d] <- getIntList
  tmp <- replicateM h getIntList
  q   <- getInt
  lrs <- replicateM q getIntList
  let m = M.fromList $ mkMap tmp 1 []
  for lrs d m M.empty
