import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List
import Data.Array

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    [h,w,d] <- getIntList
    as <- array (1, (h*w)) . flip zip [(x, y)| x <- [1..h], y <- [1..w]] . concat <$> replicateM h getIntList
    q <- getInt
    lr <- replicateM q $ do
        [l, r] <- getIntList
        return (l, r)
    let all = array (1, (h*w)) [(p, f p) | p <- [1..h*w]]
            where f p | p+d > h*w = 0
                      | otherwise = all ! (p + d) + abs (x1 - x0) + abs (y1 - y0)
                    where (x0, y0) = as ! p
                          (x1, y1) = as ! (p + d)
    forM_ lr $ \(l, r) -> do
        print $ all ! l - all ! r