import           Control.Monad      (forM, replicateM)
import           Data.Array.Unboxed
import           Debug.Trace

type Dat = Array Int (Int, Int)

main :: IO ()
main = do
    [h, w, d] <- getInts
    ass <- fmap concat $
           forM [1..h] $
           \i -> flip zip (flip zip [1..] $ repeat i)
                 <$> getInts
    q <- readLn :: IO Int
    qs <- replicateM q getTuple
    let dat = array (1, h*w) ass :: Dat
        acc = array (1, h*w) $ accumSum d (h*w) dat :: Array Int Int
    mapM_ print $ solve qs acc
    return ()

getInts :: IO [Int]
getInts = map read . words <$> getLine

getTuple :: IO (Int, Int)
getTuple = do
    [l, r] <- getInts
    return (l, r)

solve qs as = map f qs
    where
      f (l, r) = as ! r - as ! l

accumSum :: Int -> Int -> Dat -> [(Int, Int)]
accumSum d lim dat = concatMap oneLine [1..d]
    where
      accSum ls = zipWith (+) ls $ 0 : accSum ls
      oneLine k = let ns = f k
                      ms = map (dat !) ns
                      ls = 0 : zipWith dst ms (tail ms)in
                    zip ns $ accSum ls
      dst (c1, r1) (c2, r2) = abs (c1 - c2) + abs (r1 - r2)
      f m = takeWhile (<= lim) [m, m + d..]
