import std.stdio;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;

void main()
{
    long H, W, D;
    {
        auto input = readln.chomp.split(' ').map!(to!long);
        H = input[0];
        W = input[1];
        D = input[2];
    }
    long[2][] table = new long[2][](H * W + 1);
    foreach (h; 0 .. H)
    {
        auto input = readln.chomp.split(' ').map!(to!long).array;
        foreach (w, n; input)
        {
            table[n] = [h, w];
        }
    }
    auto Q = readln.chomp.to!long;
    foreach (_; 0 .. Q)
    {
        long L, R;
        {
            auto input = readln.chomp.split(' ').map!(to!long);
            L = input[0];
            R = input[1];
            table.memoize!calc(L, R, D).writeln();
        }
    }
}

long calc(ref long[2][] table, long L, long R, long D)
{
    if (L == R)
        return 0;
    auto from = table[L];
    auto to = table[L + D];
    long cost = abs(from[0] - to[0]) + abs(from[1] - to[1]);
    return table.memoize!calc(L + D, R, D) + cost;
}
