Language: C++

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fo(a,b) for(int a=0;a<b;a++)
#define Sort(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define fi first
#define se second
#define co(a) cout<<a<<endl
#define sz size
#define bgn begin()
#define en end()
#define pb(a) push_back(a)
#define pop pop_back
#define V vector
#define P pair
template<class T>
 void cou(vector<vector<T>> a){
  int b=a.size();
  int c=a[0].size();
  fo(i,b){
    fo(j,c){
      cout<<a[i][j];
      if(j==c-1)
        cout<<endl;
      else
        cout<<' ';
    }
  }
}
//template<>
 void cou(vector<vector<char>> a){
  int b=a.size();
  int c=a[0].size();
  fo(i,b){
    fo(j,c){
      cout<<a[i][j];
      if(j==c-1)
        cout<<endl;
      else
        cout<<' ';
    }
  }
}
 int wari(int a,int b) {
   if(a%b==0)
     return a/b;
   else
     return a/b+1;
 }
 int keta(int a){
  double b=a;
  b=log10(b);
  int c=b;
  return c+1;
 }
 int souwa(int a){
  return a*(a+1)/2;
 }
 int lcm(int a,int b){
  int d=a,e=b,f;
  if(a<b)
    swap(a,b);
  int c,m=1;
  while(m){
    c=a%b;
    if(c==0){
       f=b;
      m--;
    }
    else{
      a=b;
      b=c;
 }
  }
  return d*e/f;
    }
  int gcm(int a,int b){
      int d=a,e=b,f;
  if(a<b)
    swap(a,b);
  int c,m=1;
  while(m){
    c=a%b;
    if(c==0){
       f=b;
      m--;
    }
    else{
      a=b;
      b=c;
 }
  }
  return f;
  }
 bool prime(int a){
   if(a<2)
     return false;
   else if(a==2)
     return true;
   else if(a%2==0)
     return false;
   double b=sqrt(a);
    for(int i=3;i<=b;i+=2){
      if(a%i==0){
        return false;
      }
    }
   return true;
 }
struct Union{
  vector<int> par;
  Union(int n){
	par= vector<int>(n, -1);
  }
  int find(int x){
    if(par[x]<0)
      return x;
    else
      return par[x]=find(par[x]);
  }
  bool same(int a,int b){
    return find(a)==find(b);
  }
  int Size(int a){
    return -par[find(a)];
  }
  void unite(int a,int b){
    a=find(a);
    b=find(b);
    if(a==b)
      return;
    if(Size(b)>Size(a))
      swap<int>(a,b);
    par[a]+=par[b];
    par[b]=a;
  }
};
int ketas(int a){
  string b=to_string(a);
  int c=0;
  fo(i,keta(a)){
    c+=b[i]-'0';
  }
  return c;
}
signed main(){
  int a,b,c;
  cin>>a>>b>>c;
  vector<vector<int>> d(a,vector<int>(b));
  fo(i,a){
    fo(j,b)
      cin>>d[i][j];
  }
  int e;
  cin>>e;
  vector<pair<int,int>> f(e);
  fo(i,e)
    cin>>f[i].fi>>f[i].se;
  V<int> g(a*b,0);
  V<P<int,int>> z(a*b);
  int x=0,y=0;
  int x1,x2,y2,y1;
  fo(i,a){
    fo(j,b)
      z[d[i][j]-1].fi=i,z[d[i][j]-1].se=j;
  for(int i=c;i<a*b;i++){
    g[i]=g[i-c]+abs(z[i-c].fi-z[i].fi)+abs(z[i-c].se-z[i].se)
  fo(i,e){
    if(f[i].se==f[i].fi)
      cout<<0<<endl;
    else{
      cout<<g[f[i].se-1]-g[f[i].fi-1]<<endl;
    }
  }
  cout<<g[3]<<' '<<g[5]<<' '<<g[7]<<endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:168:62: error: expected ';' before 'for'
  168 |     g[i]=g[i-c]+abs(z[i-c].fi-z[i].fi)+abs(z[i-c].se-z[i].se)
      |                                                              ^
      |                                                              ;
a.cc:4:36: error: expected ';' before ')' token
    4 | #define fo(a,b) for(int a=0;a<b;a++)
      |                                    ^
a.cc:169:3: note: in expansion of macro 'fo'
  169 |   fo(i,e){
      |   ^~
a.cc:177:2: error: expected '}' at end of input
  177 | }
      |  ^
a.cc:164:10: note: to match this '{'
  164 |   fo(i,a){
      |          ^
a.cc:177:2: error: expected '}' at end of input
  177 | }
      |  ^
a.cc:147:14: note: to match this '{'
  147 | signed main(){
      |              ^