#include <bits/stdc++.h>
#define range(i, l, r) for(int (i) = (l); (i) < (r); (i)++)
#define reversed_range(i, l, r) for (int (i) = (r) - 1; (i) >= l; (i)--)
using namespace std;
template <typename T>
using vec = vector<T>;
using lint = long long;
using ulint = unsigned long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;

template <typename S, typename T>
ostream& operator <<(ostream& os, pair<S, T> p) {
    os << "(";
    os << p.first << ", " << p.second;
    return os << ")";
}

template <typename T>
ostream& operator <<(ostream& os, vec<T> v) {
    os << "[";
    for (int i = 0; i < v.size() - 1; i++) {
        os << v.at(i) << ", ";
    }
    return os << v.at(v.size() - 1) << "]";
}

template <typename T>
ostream& operator <<(ostream& os, set<T>& s) {
    os << "{";
    auto it_first_item = s.begin();
    cout << *it_first_item;
    for (auto it = ++it_first_item; it != s.end(); it++) {
        cout << ", " << *it;
    }
    return cout << "}";
}

template <typename T>
ostream& operator <<(ostream& os, unordered_set<T>& s) {
    os << "{";
    auto it_first_item = s.begin();
    cout << *it_first_item;
    for (auto it = ++it_first_item; it != s.end(); it++) {
        cout << ", " << *it;
    }
    return cout << "}";
}

template <typename K, typename V>
ostream& operator <<(ostream& os, map<K, V> m) {
    os << "{";
    auto it_first_item = m.begin();
    cout << it_first_item->first << ": " << it_first_item->second;
    for (auto it = ++it_first_item; it != m.end(); it++) {
        cout << ", " << it->first << ": " << it->second;
    }
    return os << "}";
}

template <typename K, typename V>
ostream& operator <<(ostream& os, unordered_map<K, V> m) {
    os << "{";
    auto it_first_item = m.begin();
    cout << it_first_item->first << ": " << it_first_item->second;
    for (auto it = ++it_first_item; it != m.end(); it++) {
        cout << ", " << it->first << ": " << it->second;
    }
    return os << "}";
}

lint cost(plint from, plint to) {
    return abs(from.first - to.first) + abs(from.second - to.second);
}

int main() {
    cin.tie(0); cout.tie(0);
    ios::sync_with_stdio(false);
    lint H, W, D, Q;
    cin >> H >> W >> D;
    vec<vec<lint>> grid(H, vec<lint>(W, 0));
    range(i, 0, H) range(j, 0, W) {
        cin >> grid.at(i).at(j);
    }
    cin >> Q;
    vec<plint> queries;
    range(i, 0, Q) {
        lint l, r;
        cin >> l >> r;
        queries.emplace_back(l, r);
    }
    // preprocessing. O(HW)
    vec<plint> coord_of(H * W + 1, {-1, -1});
    range(i, 0, H) range(j, 0, W) {
        lint value = grid.at(i).at(j);
        coord_of.at(value) = {i, j};
    }
    vec<lint> cost_to(H * W + 1, -1);
    lint ub_value = H * W;
    range(start, 0, D) {
        cost_to.at(start) = 0;
        lint cur_value = start;
        while (cur_value <= ub_value - D) {
            lint next_value = cur_value + D;
            plint from = coord_of.at(cur_value);
            plint to = coord_of.at(next_value);
            cost_to.at(next_value) = cost_to.at(cur_value) + cost(from, to);
            cur_value = next_value;
        }
    }
    // cout << cost_to << "\n";
    for (const auto query : queries) {
        lint l = query.first, r = query.second;
        cout << cost_to.at(r) - cost_to.at(l) << "\n";
    }
}

Measured memory_bytes: 5496