#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
#include <bits/stdc++.h>
using namespace std;

#if defined(_WIN32) || defined(_WIN64)
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#elif defined(__GNUC__)
#else
#define getchar_unlocked getchar
#define putchar_unlocked putchar
#endif
namespace {
#define isvisiblechar(c) (0x21<=(c)&&(c)<=0x7E)
    class Scanner {
    public:
        template<typename T> void input_integer(T& var) {
            var = 0; T sign = 1;
            int cc = getchar_unlocked();
            for (; cc<'0' || '9'<cc; cc = getchar_unlocked())
                if (cc == '-') sign = -1;
            for (; '0' <= cc && cc <= '9'; cc = getchar_unlocked())
                var = (var << 3) + (var << 1) + cc - '0';
            var = var * sign;
        }
        inline int c() { return getchar_unlocked(); }
        inline Scanner& operator>>(int& var) { input_integer<int>(var); return *this; }
        inline Scanner& operator>>(long long& var) { input_integer<long long>(var); return *this; }
        inline Scanner& operator>>(string& var) {
            int cc = getchar_unlocked();
            for (; !isvisiblechar(cc); cc = getchar_unlocked());
            for (; isvisiblechar(cc); cc = getchar_unlocked())
                var.push_back(cc);
            return *this;
        }
        template<typename IT> void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; }
    };
    class Printer {
    public:
        template<typename T>
        void output_integer(T var) {
            if (var == 0) { putchar_unlocked('0'); return; }
            if (var < 0)
                putchar_unlocked('-'),
                var = -var;
            char stack[32]; int stack_p = 0;
            while (var)
                stack[stack_p++] = '0' + (var % 10),
                var /= 10;
            while (stack_p)
                putchar_unlocked(stack[--stack_p]);
        }
        inline Printer& operator<<(char c) { putchar_unlocked(c); return *this; }
        inline Printer& operator<<(int var) { output_integer<int>(var); return *this; }
        inline Printer& operator<<(long long var) { output_integer<long long>(var); return *this; }
        inline Printer& operator<<(char* str_p) { while (*str_p) putchar_unlocked(*(str_p++)); return *this; }
        inline Printer& operator<<(const string& str) {
            const char* p = str.c_str();
            const char* l = p + str.size();
            while (p < l) putchar_unlocked(*p++);
            return *this;
        }
        template<typename IT> void join(IT begin, IT end, char sep = '\n') { for (auto it = begin; it != end; ++it) *this << *it << sep; }
    };
}
Scanner scanner;
Printer printer;

main() {
    int h, w, d, a, q, l, r;
    vector<pair<int, int>> pow(90001);
    int sum[90001] = {};
    scanner >> h >> w >> d;
    for(int y = 0; y < h; y++) {
        for(int x = 0; x < w; x++) {
            scanner >> a;
            pow[a] = make_pair(x, y);
        }
    }
    for(int i = d; i <= h*w; i++) {
        sum[i] = sum[i - d] + abs(pow[i].first - pow[i - d].first) + abs(pow[i].second - pow[i - d].second);
    }
    scanner >> q;
    while(q--) {
        scanner >> l >> r;
        printer << sum[r] - sum[l] << '\n';
    }
}


Measured memory_bytes: 2176