#[allow(unused_macros)]
macro_rules! parse_line {
    ( $t:ty ) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            iter.next().unwrap().parse::<$t>().unwrap()
        }
    );

    ( $( $t:ty), +) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ( $(iter.next().unwrap().parse::<$t>().unwrap()),* )
        }
    );
}

#[allow(unused_macros)]
macro_rules! read_line {
    () => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            line.pop();
            line
        }
    )
}

#[allow(unused_macros)]
macro_rules! parse_vec {
    ( $t:ty ) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let iter = line.split_whitespace();
            iter.map(|v| v.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    )
}

#[allow(dead_code)]
fn parse_table_ws<T>(n: usize) -> Vec<Vec<T>>
where T: std::str::FromStr, <T as std::str::FromStr>::Err: std::fmt::Debug
{
    (0..n).map(|_| read_line!().split_whitespace().map(|s| s.parse().unwrap()).collect::<Vec<_>>())
        .collect::<Vec<Vec<_>>>()
}

fn solve(d: i32, aa: Vec<Vec<i32>>, lrs: Vec<(i32, i32)>) -> Vec<i32> {

    let mut m = std::collections::HashMap::new();

    for (r, v) in aa.iter().enumerate() {
        for (c, a) in v.iter().enumerate() {
            m.insert(a, (r as i32,c as i32));
        }
    }


    let h = aa.len() as i32;
    let w = aa[0].len() as i32;

    let mut dd = vec![0; 300*300+1];
    for i in (d+1)..(h*w+1) {
        let &(xs, ys) = m.get(&(i-d)).unwrap();
        let &(x, y) = m.get(&i).unwrap();
        dd[i as usize] = dd[(i-d) as usize] + (xs-x).abs() + (ys-y).abs();
    }

    let mut ans = Vec::new();

    for (l, r) in lrs {
        ans.push(dd[r as usize]-dd[l as usize]);
    }

    ans
}

fn main() {
    let (h, _w, d) = parse_line!(usize, usize, i32);
    let aa = parse_table_ws(h);
    let q = parse_line!(usize);
    let lrs = (0..q).map(|_| parse_line!(i32, i32)).collect::<Vec<_>>();

    for v in solve(d, aa, lrs) {
        println!("{}", v);
    }
}

#[test]
fn solve_test() {
    assert_eq!(solve(2, vec![vec![1,4,3],
                             vec![2,5,7],
                             vec![8,9,6]],
                     vec![(4,8)]), vec![5]);

    assert_eq!(solve(3, vec![vec![3,7],
                             vec![1,4],
                             vec![5,2],
                             vec![6,8]],
                     vec![(2,2), (2,2)]), vec![0,0]);

    assert_eq!(solve(4, vec![vec![13,25,7,15,17],
                             vec![16,22,20,2,9],
                             vec![14,11,12,1,19],
                             vec![10,6,23,8,18],
                             vec![3,21,5,24,4]],
                     vec![(13,13), (2,10), (13,13)]), vec![0,5,0]);

    assert_eq!(solve(1,
                     (0..300).map(|i| ((i*300+1)..((i+1)*300+1)).collect::<Vec<_>>()).collect::<Vec<Vec<_>>>(),
                     vec![(1,90000)]), vec![(299+300)*299 + 299]);
}


Measured memory_bytes: 12540