import Control.Applicative
import Control.Monad
import qualified Data.IntMap as IM
import qualified Data.Map as M
import qualified Data.Set as S

main :: IO()
main = do
    [h,w,d] <- map read . words <$> getLine
    ass <- replicateM h $ map read . words <$> getLine
    q <- readLn
    lrs <- replicateM q $ map read . words <$> getLine
    let m = fromll ass IM.empty 0
    mapM_ (print . solve m d (h*w)) lrs
    where
        fromll [] m _ = m
        fromll (xs:xss) m i = fromll xss (froml xs m i 0) (i+1)
        froml [] m _ _ = m
        froml (x:xs) m i j = froml xs (IM.insert x (i,j) m) i (j+1)
        solve m d u [l,r] = if r == l then 0 else table r - table l
            where
                table = (M.fromSet f (S.fromList [(i,j) | i <- [1..d], j <- [0..(u-i) `div` d]]) M.!) . conv
                conv x = ((x-1) `mod` d + 1,(x-1) `div` d)
                conv' (x,y) = x + y * d
                f (_,0) = 0
                f (x,y) = cost (z-d) z + table (z-d)
                    where z = conv' (x,y)
                cost a b = abs (ax-bx) + abs (ay-by)
                    where
                        (ax,ay) = m IM.! a
                        (bx,by) = m IM.! b
