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

void main() {
    long H, W, D;
    readf("%d %d %d\n", &H, &W, &D);

    long[] x = new long[H*W];
    long[] y = new long[H*W];

    foreach (j; 0..H) {
        auto A = readln.split.to!(long[]);
        foreach (i; 0..W) {
            x[A[i]-1] = i;
            y[A[i]-1] = j;
        }
    }
    auto Q = readln.chomp.to!long;
    auto Ls = new long[Q];
    auto Rs = new long[Q];
    foreach (i; 0..Q) {
        long L, R;
        readf("%d %d\n", &L, &R);
        Ls[i] = L-1;
        Rs[i] = R-1;
    }
    foreach (i; 0..Q) {
        auto L = Ls[i];
        auto R = Rs[i];
        long res = 0;
        for (auto c = L; c < R; c += D) {
            res += abs(x[c+D] - x[c]) + abs(y[c+D] - y[c]);
        }
        writeln(res);
    }
}

