Language: C++

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <climits>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++ )
#define repf(i,m,n) for(int (i)=m;(i)<n;(i)++)
#define all(v) (v).begin(), (v).end()
#define ll long long
#define vec(name,num) vector<ll> name((num),0);
#define op(i) cout<<(i)<<endl;
#define opf(i) cout<<fixed<<setprecision(10)<<(i)<<endl;
#define ip(i) cin>>(i);
#define opN cout<<"No"<<endl;
#define opY cout<<"Yes"<<endl;
#define opP cout<<"Possible"<<endl;
#define opI cout<<"Impossible"<<endl;
#define mat(name,fnum,snum); vector<vector<ll>> name((fnum),vector<ll>((snum),0));
#define matC(name,fnum,snum); vector<vector<char>> name((fnum),vector<char>((snum),0));
#define debugP int debug_point; cin>>debug_point;
#define FI first
#define SE second
const ll MOD= 1e9 + 7;

mat(a,300,300);
vector<ll> num_to_mat_x(90001);
vector<ll> num_to_mat_y(90001);

ll dfs(ll now, ll cost,ll d, ll goal){
    if(now==goal){
        return cost;
    }
    dfs(now+d,abs(num_to_mat_x[now+d]-num_to_mat_x[now])+abs(num_to_mat_y[now]-num_to_mat_y[now+d])+cost,d,goal);
}

void solve(long long H, long long W, long long D, std::vector<std::vector<long long>> A, long long Q, std::vector<long long> L, std::vector<long long> R){
    a=A;
    rep(i,Q){
        //op(dfs(L[i],0,D,R[i]))
        ll cost=0;
        ll now=L[i];
        ll goal=R[i];
        while(1){
            if(goal==now)break;
            cost+=abs(num_to_mat_x[now+D]-num_to_mat_x[now])+abs(num_to_mat_y[now]-num_to_mat_y[now+D]);
            now+=D;
        }
        op(cost)
    }
    
}

// Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long H;
    scanf("%lld",&H);
    long long W;
    scanf("%lld",&W);
    long long D;
    scanf("%lld",&D);
    std::vector<std::vector<long long>> A(H, std::vector<long long>(W));
    for(int i = 0 ; i < H ; i++){
        for(int j = 0 ; j < W ; j++){
            scanf("%lld",&A[i][j]);
            num_to_mat[A[i][j]] = make_pair(i,j);
        }
        
    }
    long long Q;
    scanf("%lld",&Q);
    std::vector<long long> L(Q);
    std::vector<long long> R(Q);
    for(int i = 0 ; i < Q ; i++){
        scanf("%lld",&L[i]);
        scanf("%lld",&R[i]);
    }
    solve(H, W, D, std::move(A), Q, std::move(L), std::move(R));
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:72:13: error: 'num_to_mat' was not declared in this scope; did you mean 'num_to_mat_y'?
   72 |             num_to_mat[A[i][j]] = make_pair(i,j);
      |             ^~~~~~~~~~
      |             num_to_mat_y
a.cc: In function 'long long int dfs(long long int, long long int, long long int, long long int)':
a.cc:40:8: warning: control reaches end of non-void function [-Wreturn-type]
   40 |     dfs(now+d,abs(num_to_mat_x[now+d]-num_to_mat_x[now])+abs(num_to_mat_y[now]-num_to_mat_y[now+d])+cost,d,goal);
      |     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~