import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;

import std.math;

void main() {
    int h, w, d;
    scan(h, w, d);

    auto a = iota(h).map!(i => readln.split.to!(int[])).array;
    foreach (i ; 0 .. h) a[i][] -= 1;

    auto r = new int[](h*w);
    auto c = new int[](h*w);

    foreach (i ; 0 .. h) {
        foreach (j ; 0 .. w) {
            r[a[i][j]] = i;
            c[a[i][j]] = j;
        }
    }

    int cost(int i, int j) {
        return abs(r[i] - r[j]) + abs(c[i] - c[j]);
    }

    auto dp = new long[](h*w);
    foreach_reverse (i ; 0 .. h*w - d) {
        dp[i] = dp[i+d] + cost(i, i+d);
    }

    debug {
        writeln(dp);
    }

    int q;
    scan(q);

    while (q--) {
        int li, ri;
        scan(li, ri);
        li--, ri--;
        writeln(dp[li] - dp[ri]);
    }
}


void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
