#![allow(unused_imports)]

use std::{cmp, fmt};
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
use std::io::{self, BufWriter, Read, StdinLock, Write};
use std::ops::{Add, Div, Mul, Sub};
use std::str::{self, FromStr};

macro_rules! eprintln {
    ($($x: tt)*) => (io::stderr().write_fmt(format_args!($($x)*)).unwrap())
}

fn main() {
    let (c, d, lrs) = {
        let stdin = io::stdin();
        let mut sc = Scanner::new(stdin.lock().bytes());
        let (h, w, d) = (sc.next(), sc.next(), sc.next());
        let c = cumulate(&sc.mat(h, w), d);
        let q = sc.next();
        (c, d, sc.pairs::<usize, usize>(q))
    };
    for (l, r) in lrs {
        let c = &c[l % d];
        println!("{}", c[r / d] - c[l / d]);
    }
}

fn cumulate(a: &Vec<Vec<usize>>, d: usize) -> Vec<Vec<usize>> {
    let a = {
        let mut r = vec![(0, 0); 90001];
        for (y, l) in a.iter().enumerate() {
            for (x, v) in l.iter().enumerate() {
                r[*v] = (x, y);
            }
        }
        r
    };
    let mut r = vec![Vec::with_capacity(90001); d];
    for (u, l) in r.iter_mut().enumerate() {
        let mut s = 0;
        let mut i = d + u;
        l.push(0);
        while i <= 90000 {
            s += distance(a[i], a[i - d]);
            l.push(s);
            i += d;
        }
    }
    r
}

fn distance(p1: (usize, usize), p2: (usize, usize)) -> usize {
    let ((x1, y1), (x2, y2)) = (p1, p2);
    (if x1 > x2 { x1 - x2 } else { x2 - x1 }) + (if y1 > y2 { y1 - y2 } else { y2 - y1 })
}

struct Scanner<R: Read> {
    buf: Vec<u8>,
    bytes: io::Bytes<R>,
}

#[allow(dead_code)]
impl<R: Read> Scanner<R> {
    fn new(bytes: io::Bytes<R>) -> Self {
        Scanner {
            buf: Vec::with_capacity(1024),
            bytes: bytes,
        }
    }

    fn next<T: FromStr>(&mut self) -> T
    where
        <T as FromStr>::Err: fmt::Debug,
    {
        let buf = &mut self.buf;
        buf.clear();
        self.bytes
            .by_ref()
            .map(Result::unwrap)
            .skip_while(|&c| c == b' ' || c == b'\n')
            .take_while(|&c| !(c == b' ' || c == b'\n'))
            .foreach(|c| buf.push(c));
        str::from_utf8(buf).unwrap().parse().unwrap()
    }

    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T>
    where
        <T as FromStr>::Err: fmt::Debug,
    {
        (0..n).map(|_| self.next()).collect()
    }

    fn pairs<T1: FromStr, T2: FromStr>(&mut self, n: usize) -> Vec<(T1, T2)>
    where
        <T1 as FromStr>::Err: fmt::Debug,
        <T2 as FromStr>::Err: fmt::Debug,
    {
        (0..n).map(|_| (self.next(), self.next())).collect()
    }

    fn trios<T1: FromStr, T2: FromStr, T3: FromStr>(&mut self, n: usize) -> Vec<(T1, T2, T3)>
    where
        <T1 as FromStr>::Err: fmt::Debug,
        <T2 as FromStr>::Err: fmt::Debug,
        <T3 as FromStr>::Err: fmt::Debug,
    {
        (0..n)
            .map(|_| (self.next(), self.next(), self.next()))
            .collect()
    }

    fn mat<T: FromStr>(&mut self, m: usize, n: usize) -> Vec<Vec<T>>
    where
        <T as FromStr>::Err: fmt::Debug,
    {
        (0..m).map(|_| self.vec(n)).collect()
    }

    fn strings_as_mat<T, F: FnMut(u8) -> T>(&mut self, h: usize, mut f: F) -> Vec<Vec<T>> {
        (0..h)
            .map(|_| {
                self.bytes
                    .by_ref()
                    .map(Result::unwrap)
                    .skip_while(|&c| c == b' ' || c == b'\n')
                    .take_while(|&c| !(c == b' ' || c == b'\n'))
                    .map(&mut f)
                    .collect()
            })
            .collect()
    }
}

trait Foreach {
    type Item;
    fn foreach<F: FnMut(Self::Item)>(self, f: F);
}

impl<T, I: Iterator<Item = T>> Foreach for I {
    type Item = T;

    fn foreach<F: FnMut(T)>(self, mut f: F) {
        self.fold((), move |(), x| f(x));
    }
}


Measured memory_bytes: 12656