Language: C++

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll H, W, D;
    ll Q;
    vector<ll, pair<ll, ll>> A;
    cin >> H >> W >> D;
    A.resize(H * W);
    rep(i, H) rep(j, W) {
        ll temp;
        cin >> temp;
        A[temp] = {i, j};
    }
    cin >> Q;
    rep(i, Q) {
        ll cost = 0;
        ll L, R;
        cin >> L >> R;
        while(L != R) {
            pair<ll, ll> current;
            current = A[L];
            cost += abs(current.first - A[L + D].first) + abs(current.second - A[L + D].second);
            L += D;
        }
        cout << cost << endl;
    }
    return 0;
}


Compiler output:
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'struct std::_Vector_base<long long int, std::pair<long long int, long long int> >':
/usr/include/c++/14/bits/stl_vector.h:428:11:   required from 'class std::vector<long long int, std::pair<long long int, long long int> >'
  428 |     class vector : protected _Vector_base<_Tp, _Alloc>
      |           ^~~~~~
a.cc:48:30:   required from here
   48 |     vector<ll, pair<ll, ll>> A;
      |                              ^
/usr/include/c++/14/bits/stl_vector.h:87:28: error: no type named 'value_type' in 'struct std::pair<long long int, long long int>'
   87 |         rebind<_Tp>::other _Tp_alloc_type;
      |                            ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:89:9: error: no type named 'value_type' in 'struct std::pair<long long int, long long int>'
   89 |         pointer;
      |         ^~~~~~~
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'class std::vector<long long int, std::pair<long long int, long long int> >':
a.cc:48:30:   required from here
   48 |     vector<ll, pair<ll, ll>> A;
      |                              ^
/usr/include/c++/14/bits/stl_vector.h:518:20: error: '_M_allocate' has not been declared in 'std::vector<long long int, std::pair<long long int, long long int> >::_Base'
  518 |       using _Base::_M_allocate;
      |                    ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:519:20: error: '_M_deallocate' has not been declared in 'std::vector<long long int, std::pair<long long int, long long int> >::_Base'
  519 |       using _Base::_M_deallocate;
      |                    ^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:521:20: error: '_M_get_Tp_allocator' has not been declared in 'std::vector<long long int, std::pair<long long int, long long int> >::_Base'
  521 |       using _Base::_M_get_Tp_allocator;
      |                    ^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/cassert:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:33:
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = long long int; _Alloc = std::pair<long long int, long long int>]':
a.cc:48:30:   required from here
   48 |     vector<ll, pair<ll, ll>> A;
      |                              ^
/usr/include/c++/14/bits/stl_vector.h:136:24: error: no type named 'value_type' in 'struct std::pair<long long int, long long int>'
  136 |         _Vector_impl() _GLIBCXX_NOEXCEPT_IF(
      |                        ^~~~~~~~~~~~~~~~~~~~
a.cc: In function 'int main()':
a.cc:54:10: error: no match for 'operator[]' (operand types are 'std::vector<long long int, std::pair<long long int, long long int> >' and 'll' {aka 'long long int'})
   54 |         A[temp] = {i, j};
      |          ^
a.cc:63:24: error: no match for 'operator[]' (operand types are 'std::vector<long long int, std::pair<long long int, long long int> >' and 'll' {aka 'long long int'})
   63 |             current = A[L];
      |                        ^
a.cc:64:42: error: no match for 'operator[]' (operand types are 'std::vector<long long int, std::pair<long long int, long long int> >' and 'll' {aka 'long long int'})
   64 |             cost += abs(current.first - A[L + D].first) + abs(current.second - A[L + D].second);
      |                                          ^
a.cc:64:81: error: no match for 'operator[]' (operand types are 'std::vector<long long int, std::pair<long long int, long long int> >' and 'll' {aka 'long long int'})
   64 |             cost += abs(current.first - A[L + D].first) + abs(current.second - A[L + D].second);
      |                                                                                 ^
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = long long int; _Alloc = std::pair<long long int, long long int>]':
/usr/include/c++/14/bits/stl_vector.h:314:7:   required from here
  314 |       _Vector_base() = default;
      |       ^~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:141:26: error: no type named 'value_type' in 'struct std::pair<long long int, long long int>'
  141 |         : _Tp_alloc_type()
      |                          ^