import Control.Applicative ((<$>))
import Control.Monad (replicateM_, forM_)

import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as B

import Data.Char (isSpace)

import Data.Vector (Vector, (!))
import qualified Data.Vector as V
import Data.Vector.Mutable (IOVector)
import qualified Data.Vector.Mutable as VM

main :: IO ()
main = do
  [h, w, d] <- map read <$> words <$> getLine
  tbl <- VM.new (h * w + 1) :: IO (IOVector (Int, Int))
  forM_ [0 .. h-1] $ \i -> do
    tv <- getl (readiv B.readInt)
    forM_ [0 .. w-1] $ \j -> do
      VM.write tbl (tv ! j) (i, j)
  q <- readLn :: IO Int
  replicateM_ q $ do
    [l, r] <- map read <$> words <$> getLine :: IO [Int]
    loop tbl d 0 l r >>= print
  where
    loop :: IOVector (Int, Int) -> Int -> Int -> Int -> Int -> IO Int
    loop v d c s t | s == t = return c
                   | otherwise = do
                       ix1 <- VM.read v s
                       ix2 <- VM.read v (s + d)
                       loop v d (c + md ix1 ix2) (s + d) t

getl :: (ByteString -> a) -> IO a
getl f = f <$> B.getLine

readiv :: Integral a =>  (ByteString -> Maybe (a, ByteString)) -> ByteString -> Vector a
readiv f = V.unfoldr g
  where
    g s = do
      (n, s') <- f s
      return (n, B.dropWhile isSpace s')

md :: (Int, Int) -> (Int, Int) -> Int
md (x1, y1) (x2, y2) = abs (x1 - x2) + abs (y1 - y2)
