{-# LANGUAGE BangPatterns, MultiWayIf #-}
import Control.Applicative((<$>))
import Control.Monad
-- import Data.Map ((!))
-- import qualified Data.Map as M
import Data.IntMap ((!))
import qualified Data.IntMap 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 }

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

sub (Pos x y) (Pos x' y') = abs (x-x') + abs (y-y')

mkTable x to d m cach
  | x == to+1 = cach
  | x <= d    = mkTable (x+1) to d m $ M.insert x 0 cach
  | otherwise =
      let
        !d_pre   = cach ! (x-d)
        !pos_pre = m ! (x-d)
        !pos     = m ! x
        !cost    = d_pre + sub pos_pre pos
      in
        mkTable (x+1) to d m $ M.insert x cost 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 []
      table = mkTable 1 (h*w) d m M.empty
  forM_ lrs $ \[l, r] -> do
    print $ table ! r - table ! l
