import std.stdio, std.algorithm, std.string, std.string, std.conv, std.array, std.range, std.math;
int readint() { return readln.chomp.to!int; }
int[] readints() { return readln.split.to!(int[]); }

void main() {
    auto hwd = readints();
    int h = hwd[0], w = hwd[1], d = hwd[2];
    int[] px = new int[h*w+5];
    int[] py = new int[h*w+5];
    foreach(y; 0..h) {
        auto a = readints();
        foreach(x, v; a) {
            px[v] = x;
            py[v] = y;
        }
    }

    int[] ds = new int[h*w+5];
    foreach_reverse(i; 0..h*w-d+1) {
        ds[i] = ds[i+d] + abs(px[i] - px[i+d]) + abs(py[i] - py[i+d]);
    }
    
    int q = readint();
    while(q--) {
        int[] lr = readints();
        writeln(ds[lr[0]] - ds[lr[1]]);
    }
    string _ = readln(); // dbg
}