stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <iostream>

#include <sstream>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <ctime>

#include <cstring>

#include <string>

#include <vector>

#include <stack>

#include <queue>

#include <deque>

#include <map>

#include <set>

#include <bitset>

#include <numeric>

#include <utility>

#include <iomanip>

#include <algorithm>

#include <functional>

using namespace std;



typedef long long ll;

typedef vector<int> vint;

typedef vector<long long> vll;

typedef pair<int,int> pint;

typedef pair<long long, long long> pll;



#define MP make_pair

#define PB push_back

#define ALL(s) (s).begin(),(s).end()

#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl



template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 

{ return s << '<' << P.first << ", " << P.second << '>'; }

template<class T> ostream& operator << (ostream &s, vector<T> P) 

{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }

template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 

{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }





const int MAX = 200000;

const ll INF = 1LL<<61;



int n, k;

long long a[MAX];



int main() {

    while (cin >> n >> k) {

        if (n == 0) break;

        memset(a, 0, sizeof(a));

        for (int i = 0; i < n; ++i) cin >> a[i+1];

        for (int i = 0; i <= n; ++i) a[i+1] += a[i];

        

        long long res = -INF;

        for (int i = 0; i + k <= n; ++i) {

            chmax(res, a[i+k] - a[i]);

        }

        cout << res << endl;

    }

    return 0;

}


// Optimized solution to the same problem (faster):
#include <iostream>

#include <vector>

using namespace std;

const long long INF = 1LL<<60; // 仮想的な無限大の値



int main() {

    // 入力

    int N, K;

    while (cin >> N >> K) {

        if (N == 0) break;

        vector<long long> a(N);

        for (int i = 0; i < N; ++i) cin >> a[i];

        

        // 累積和を前計算

        vector<int> s(N+1, 0);

        for (int i = 0; i < N; ++i) s[i+1] = s[i] + a[i];

        

        // 答えを求める

        long long res = -INF; // 最初は無限小の値に初期化しておく

        for (int i = 0; i <= N-K; ++i) {

            long long val = s[K+i] - s[i];

            if (res < val) res = val;

        }

        cout << res << endl;

    }

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>



using namespace std;



int main(){

	

	while(1){

		int n,m;

		

		cin>>n>>m;

		

		if(n==0 && m ==0){

			break;

		}

		int input[100000];

		

		for(int i=0;i<n;i++){

			cin>>input[i];

		}

		

		int cont = m;

		

		int now = 0;

		for(int i=0;i<m;i++){

			now += input[i];

		}

		

		

		int max = now;

		for(int i=m;i<n;i++){

			now-=input[i-m];

			now+=input[i];

			

			if(now>max){

				max=now;

			}

		}

	

	cout<<max<<endl;

	}

	

	return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>





int main(){

	

	int n,m;

	int input[100000];

	int cont;

	int now;

	int max;

	int i;

	while(1){

		

		scanf("%d%d",&n,&m);

		if(n==0 && m ==0){

			break;

		}

		

		for(i=0;i<n;i++){

			scanf("%d",&input[i]);

		}

		

		now = 0;

		

		for(i=0;i<m;i++){

			now += input[i];

		}

		max=now;

		

		cont = m;

		

		for(i=m;i<n;i++){

			now-=input[i-m];

			now+=input[i];

			

			if(now>max){

				max=now;

			}

		}

		

		printf("%d\n",max);

	}

	

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<string.h>

using namespace std;

long long sin[100001];

int main(){

	while(1){

	memset(sin,0,sizeof(sin));

	int n,k;

	int i,j,max=0,x;

	cin>>n>>k;

		if(n==0&&k==0)break;

	for(i=0;i<n;i++){

		cin>>sin[i];

	}

	for(i=0;i<n-k+1;i++){

		x=0;

		for(j=0;j<k;j++){

			x+=sin[i+j];

		}

		if(max<x)max=x;

	}

	cout<<max<<endl;

	}

}

// Optimized solution to the same problem (faster):
#include<iostream>

using namespace std;

int main(){

	int n,k;

	while(1){

		int a[100000];

		cin>>n>>k;

		if(n==0&&k==0)break;

		int max=0,sum;

		for(int i=0;i<n;i++){

			cin>>a[i];

		}

		for(int i=0;i<n;i++){

			sum=0;

			if(i+k-1<n){

				for(int j=0;j<k;j++){

					sum+=a[i+j];

				}

				if(max<sum){

					max=sum;

				}

			}

			else break;

		}

		cout<<max<<endl;

	}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<string.h>

using namespace std;

long long sin[100001];

int main(){

	while(1){

	memset(sin,0,sizeof(sin));

	long n,k;

	int i,j,max=0,x;

		cin>>n>>k;if(n==0&&k==0)break;

	for(i=1;i<=n;i++){

		cin>>sin[i];

	}

	x=0;

	for(j=1;j<=k;j++){

		x+=sin[j];

	}

		for(i=k+1;i<=n;i++){

			x+=sin[i];

			x-=sin[i-k];

			if(x>max)max=x;

		}

		cout<<max<<endl;

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include<iostream>

using namespace std;

int main(){

	int n,k;

	while(1){

		int a[100000];

		cin>>n>>k;

		if(n==0&&k==0)break;

		int max=0,sum;

		for(int i=0;i<n;i++){

			cin>>a[i];

		}

		for(int i=0;i<n;i++){

			sum=0;

			if(i+k-1<n){

				for(int j=0;j<k;j++){

					sum+=a[i+j];

				}

				if(max<sum){

					max=sum;

				}

			}

			else break;

		}

		cout<<max<<endl;

	}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<algorithm>

using namespace std;



int main(){

  int n,k;

  int a[1000000];



  for(;;){

    cin >> n >> k;

    if(!n && !k)break;



    for(int i=0;i<n;i++)cin >> a[i];

    int sum = 0;

    for(int i=0;i<k;i++)sum += a[i];

    int m = sum;

    for(int i=k;i<n;i++){

      sum += a[i] - a[i-k];

      m = max(m,sum);

    }

    cout << m << endl;

  }

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;



int main(){

  int n,k;

  int a[1000000];



  for(;;){

    scanf("%d %d",&n,&k);

    if(!n && !k)break;



    for(int i=0;i<n;i++)scanf("%d",&a[i]);

    int sum = 0;

    for(int i=0;i<k;i++)sum += a[i];

    int m = sum;

    for(int i=k;i<n;i++){

      sum += a[i] - a[i-k];

      m = max(m,sum);

    }

    printf("%d\n",m);

  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;

typedef pair<int,int> pii;

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)

#define all(a)  (a).begin(),(a).end()

#define pb push_back

#define INF 999999999



int main(){

    double g[110][110];

    rep(i,110)rep(j,110)g[i][j]=-1;

    int n,m;

    while(cin>>n>>m){

        if(n==0&&m==0)break;

        rep(i,n){

            rep(j,n){

                cin>>g[i][j];

            }

        }

    

    

        double dp[110][110];

        rep(i,110)rep(j,110)dp[i][j] = -1;

        rep(i,n)dp[1][i]=1.0;

    

        for(int i=2;i<=m;i++){

            for(int j=0;j<n;j++){

                for(int k=0;k<n;k++){

                    dp[i][j] = max(dp[i][j],dp[i-1][k]*g[k][j]);

                }

            }

        }

        double ans = -1;

        rep(i,n){

            ans = max(ans,dp[m][i]);

        }

        printf("%.2lf\n",ans);

    }

}

// Optimized solution to the same problem (faster):
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;

typedef pair<int,int> pii;

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)

#define all(a)  (a).begin(),(a).end()

#define vi vector<int>

#define pb push_back

#define INF 999999999

//#define INF (1LL<<59)





int main(){

	int n,m;

	while(cin>>n>>m&&(n||m)){

		double table[101][101];

		rep(i,n) rep(j,n) cin>>table[i][j];

		

		double dp[101][101];

		rep(i,101)rep(j,101)dp[i][j] = -1;

		rep(i,n) dp[0][i] = 1;

		

		for(int i=1;i<m;i++){

			rep(j,n){

				rep(k,n){

					dp[i][j] = max( dp[i-1][k]*table[k][j] , dp[i][j] );

				}

			}

		}

		

		double ans = -1;

		rep(i,n){

			ans = max(ans,dp[m-1][i]);

		}

		printf("%.2lf\n",ans);

	}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <algorithm>

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define all(x) (x).begin(), (x).end()

#define ll long long

#define INF 1000000000000000000



int main() {

    int H, W, D, Q;

    cin >> H >> W >> D;

    vector<vector<ll>> A(H, vector<ll>(W));

    vector<pair<ll, ll>> ps(H * W + 93000);

    vector<ll> sum(H * W + 93000, 0);

    rep(i, H) {

        rep(j, W) {

            cin >> A[i][j];

            ps[A[i][j]] = {i, j};

        }

    }



    rep(j, H * W) {

        sum[j + D] = sum[j] + abs(ps[j + D].first - ps[j].first) +

                     abs(ps[j + D].second - ps[j].second);

    }



    cin >> Q;

    rep(i, Q) {

        int l, r;

        cin >> l >> r;

        cout << sum[r] - sum[l] << endl;

    }

}

// Optimized solution to the same problem (faster):
#include <algorithm>

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define all(x) (x).begin(), (x).end()

#define ll long long

#define ld long double

#define INF 1000000000000000000

typedef pair<ll, ll> pll;

typedef pair<int, int> pint;



int main() {

    cin.tie(0);

    ios::sync_with_stdio(false);



    int H, W, D;

    cin >> H >> W >> D;

    ll m = 0;

    map<ll, pll> ma;

    rep(i, H) {

        rep(j, W) {

            ll a;

            cin >> a;

            ma[a] = {i, j};

            m = max(m, a);

        }

    }



    vector<ll> table(m + 1, 0);

    for (int i = 0; i < m + 1; i++) {

        if (table[i] != 0)

            continue;

        for (int j = i + D; j < m + 1; j += D) {

            pll a = ma[j], b = ma[j - D];

            table[j] = table[j - D] + abs(a.first - b.first) +

                       abs(a.second - b.second);

        }

    }



    int Q;

    cin >> Q;

    vector<ll> L(Q), R(Q);

    rep(i, Q) {

        cin >> L[i] >> R[i];

        cout << table[R[i]] - table[L[i]] << endl;

    }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>



using namespace std;

typedef long long ll;



int H,W,D,A;

int Q,L,R;

map<int, pair<int, int> > pos;

int d[90001];



int  main() {

  cin >> H >> W >> D;



  for(int i=0; i<H; i++) {

    for(int j=0; j<W; j++) {

      cin >> A;

      pos[A] = make_pair(i, j);

    }

  }



  memset(d, 0, sizeof(d));

  for(int i=D+1; i<=H*W; i++) {

    d[i] = d[i - D]

      + abs(pos[i].first - pos[i - D].first)

      + abs(pos[i].second - pos[i-D].second);

  }



  cin >> Q;

  while(Q--) {

    cin >> L >> R;

    cout << d[R] - d[L] << endl;

  }

  return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>



using namespace std;

typedef long long ll;



const ll INF = 1e9;

const ll MOD = 1e9 + 7;



int main() {

  int H, W, D;

  cin >> H >> W >> D;

  vector<pair<int, int> > list(H * W + 1, pair<int, int>());

  for (int i = 0; i < H; i++) {

    for (int j = 0; j < W; j++) {

      int d;

      cin >> d;

      list[d] = make_pair(i, j);

    }

  }



  vector<ll> rw(H * W + 1, 0);

  for (int i = 1; i <= H * W - D; i++) {

    rw[i + D] = rw[i]

     + abs(list[i + D].first - list[i].first)

     + abs(list[i + D].second - list[i].second);

  }



  int Q;

  cin >> Q;

  while(Q--) {

    int l, r;

    cin >> l >> r;

    cout << rw[r] - rw[l] << endl;

  }



  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<utility>

using namespace std;

int main(){

  long long h,w,d,q,sum,bx,by;

  cin >> h >> w >> d;

  int a[h][w];

  pair<int,int> num[100000];

  for(int i=0;i<h;i++){

    for(int j=0;j<w;j++){

      cin >> a[i][j];

      num[a[i][j]] = make_pair(i,j);

    }

  }

  cin >> q;

  int l,r;

  int di[100000]={};

  pair<int,int> tmp0,tmp1;

  for(int i=d+1;i<=h*w;i++){

    tmp0 = num[i];

    tmp1 = num[i-d];

    di[i] = di[i-d]+abs(tmp0.first-tmp1.first)+abs(tmp0.second-tmp1.second);

  }

  for(int i=0;i<q;i++){

    cin >> l>>r;

    cout << di[r]-di[l] <<endl;

  }

}


// Optimized solution to the same problem (faster):
// I SELL YOU...! 

#include<iostream>

#include<vector>

#include<algorithm>

#include<functional>

#include<queue>

#include<chrono>

#include<iomanip>

#include<map>

#include<set>

#define INF 100000000000

using namespace std;

using ll = long long;

using P = pair<ll,ll>;

void init_io(){

  cin.tie(0);

  ios::sync_with_stdio(false);

  cout << setprecision(10);

}

signed main(){

  init_io();

  ll h,w,d,q;

  cin >> h >> w >> d;

  ll a[h][w],di[110000]={};

  map<ll,P> mp;

  for(int i=0;i<h;i++){

    for(int j=0;j<w;j++){

      cin >> a[i][j];

      mp[a[i][j]] =P(i,j);

    }

  }

  for(int i=d+1;i<=h*w;i++){

    di[i] = di[i-d]+abs(mp[i].first-mp[i-d].first)

      +abs(mp[i].second-mp[i-d].second);

  }

  cin >> q;

  vector<ll> l(q),r(q);

  for(int i=0;i<q;i++){

    cin >> l[i] >> r[i];

    cout << di[r[i]]-di[l[i]]<<endl;

  }

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
//   _/                _/                 _/_/_/   _/

//_/_/_/_/   _/_/   _/_/_/_/   _/_/    _/       _/_/

// _/     _/    _/   _/     _/    _/  _/_/_/     _/

//_/     _/    _/   _/     _/    _/  _/    _/   _/

// _/_/   _/_/       _/_/   _/_/      _/_/     _/

#include<iostream>

#include<algorithm>

#include<cmath>

#include<iomanip>

#include<set>

#include<map>

#include<queue>

#include<vector>

using namespace std;

typedef long long ll;

const int MOD=1e9+7;

const double pi=3.14159265358979323846;

const int inf=2e9;

const ll INF=5e18;

typedef pair<int,int> P;

int main() {

  cin.tie(0),cout.tie(0);

  ios::sync_with_stdio(false);

  ll h,w,d,a,posx[90005]={},posy[90005]={},x[90005]={},q,l,r;

  cin >> h >> w >> d;

  for(int i=0; i<h; i++) {

    for(int j=0; j<w; j++) {

      cin >> a;

      posx[a]=j,posy[a]=i;

    }

  }

  for(int i=d+1; i<=h*w; i++) {

    x[i]=x[i-d]+abs(posx[i]-posx[i-d])+abs(posy[i]-posy[i-d]);

  }

  cin >> q;

  while(q--) {

    cin >> l >> r;

    cout << x[r]-x[l] << endl;

  }

}

// Optimized solution to the same problem (faster):
/*   _/                _/                 _/_/_/   _/

  _/_/_/_/   _/_/   _/_/_/_/   _/_/    _/       _/_/

   _/     _/    _/   _/     _/    _/  _/_/_/     _/

  _/     _/    _/   _/     _/    _/  _/    _/   _/

   _/_/   _/_/       _/_/   _/_/      _/_/     _/ */

#include<iostream>

#include<algorithm>

#include<cmath>

#include<iomanip>

#include<set>

#include<map>

#include<queue>

#include<vector>

using namespace std;

using ll=long long;

const int MOD=1e9+7;

const double pi=3.14159265358979323846;

const int inf=1e9+5;

const ll INF=1e18+5;

using P=pair<int,int>;

int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

int main() {

  cin.tie(0),cout.tie(0);

  ios::sync_with_stdio(false);

  int h,w,q,x[90005]={},posx[90005]={},posy[90005]={},d,a[305][305]={},l,r;

  cin >> h >> w >> d;

  for(int i=0; i<h; i++) {

    for(int j=0; j<w; j++) {

      cin >> a[i][j];

      posy[a[i][j]]=i,posx[a[i][j]]=j;

    }

  }

  for(int i=d+1; i<=h*w; i++) {

    x[i]=x[i-d]+abs(posy[i]-posy[i-d])+abs(posx[i]-posx[i-d]);

  }

  cin >> q;

  while(q--) {

    cin >> l >> r;

    cout << x[r]-x[l] << "\n";

  }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;

#define rep2(x,fr,to) for(int (x)=(fr);(x)<(to);(x)++)

#define rep(x,to) for(int (x)=0;(x)<(to);(x)++)

#define repr(x,fr,to) for(int (x)=(fr);(x)>=(to);(x)--)

#define all(c) (c).begin(),(c).end()

#define sz(v) (int)(v).size()



typedef long long ll; typedef vector<int> VI; typedef pair<int,int> pii;

const int mod = (int)1e9 + 7;

void dbg(){ cerr << "\n"; }  

template <typename T,typename ...T2>

void dbg(const T& fst, const T2&...rst){ cerr << fst << ": "; dbg(rst...); }



//int dp[305][300];

VI ps(305*300);

int h,w, d, q;





int main()

{

	

	//cin.tie(0); ios_base::sync_with_stdio(false);

	cin >>h >>w >>d;

	vector<VI> a(h,VI(w));

	rep(i,h) rep(j,w){

		cin >> a[i][j];

		ps[a[i][j]] = i*w + j;

	}

	//memset(dp, -1, sizeof dp);

	cin >>q;

	VI vl(q), vr(q);

	rep(i,q) cin >> vl[i] >>vr[i];



	

	vector<vector<ll>> cst(d); 

	rep(i,d) cst[i].push_back(0);

	rep(i, d){

		int st = i ;//i? i: d;

		for( ;st +d <= h*w; st+=d){

			int cy= ps[st]/w, cx=ps[st]%w;

			int ny= ps[st+d]/w, nx=ps[st+d]%w;

			cst[i].push_back(cst[i].back() +abs(cy-ny)+abs(cx-nx));

		}

	}

	//rep(i,d){ for(auto x: cst[i] ) cout << x <<" "; puts("");}

	

	rep(g, q){

		int md = vl[g] % d;

		cout << cst[md][vr[g]/d] - cst[md][vl[g]/d] <<"\n";

	}

	/*

	rep(gg,q){

		int l =vl[gg],r=vr[gg];

		ll ans =0;

		

		while(l !=r ){

			int cy= ps[l]/w, cx=ps[l]%w;

			int ny= ps[l+d]/w, nx=ps[l+d]%w;

			ans += abs(cy-ny)+abs(cx-nx);

			//printf("%d:%d, ",l,l+d);

			l = l+d;

		}

		cout << ans <<"\n";

	}

	*/

	return 0;

	

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>



using namespace std;

#define rep2(x,fr,to) for(int (x)=(fr);(x)<(to);(x)++)

#define rep(x,to) for(int (x)=0;(x)<(to);(x)++)

#define repr(x,fr,to) for(int (x)=(fr);(x)>=(to);(x)--)

#define all(c) (c).begin(),(c).end()

#define sz(v) (int)(v).size()



typedef long long ll; typedef vector<int> VI; typedef pair<int,int> pii;

const int mod = (int)1e9 + 7;

void dbg(){ cerr << "\n"; }  

template <typename T,typename ...T2>

void dbg(const T& fst, const T2&...rst){ cerr << fst << ": "; dbg(rst...); }







int main()

{

	

	cin.tie(0); ios_base::sync_with_stdio(false);

	int h,w, d, q;

	cin >>h >>w >>d;

	VI ps(h*w+10);

	rep(i,h) rep(j,w){

		int a; cin >> a;

		ps[a] = i*w + j;

	}

	cin >>q;

	VI vl(q), vr(q);

	rep(i,q) cin >> vl[i] >>vr[i];



	

	vector<ll> cst(h*w+10);

	rep(i, d){

		int st = i ;

		for( ;st +d <= h*w; st+=d){

			int cy= ps[st]/w, cx=ps[st]%w;

			int ny= ps[st+d]/w, nx=ps[st+d]%w;

			cst[st+d] = cst[st] +abs(cy-ny)+abs(cx-nx);

		}

	}

	//for(auto x: cst ) cout << x <<" ";puts("");

	

	rep(g, q){

		cout << cst[vr[g]] - cst[vl[g]] <<"\n";

	}

	return 0;

	

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cmath>

#include <string>

#include<set>

#include<vector>

#define MAX 5000

using namespace std;

typedef long long ll;

typedef pair<ll, ll> point;

int main(void) {

    ll i, j, h, w, d, q, num, l, r, dis[100000];

    point posi[90001];

    cin >> h >> w >> d;

    for(i=1; i<=h; i++) {

        for(j=1; j<=w; j++) {

            cin >> num;

            posi[num] = make_pair(i, j);

        }

    }

    cin >> q;

    for(i=1; i<=h*w-d; i++)

        dis[i] = 0;

    for(i=1; i<=h*w-d; i++)

        dis[i+d] = dis[i] + abs(posi[i+d].first-posi[i].first) + abs(posi[i+d].second-posi[i].second);

        

    

    for(i=0; i<q; i++) {

        cin >> l >> r;

        cout << dis[r]-dis[l] << endl;

    }

}


// Optimized solution to the same problem (faster):
#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define pr(num) cout << num << endl

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define mkp make_pair

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 150005

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;



ll H, W, D;

ll a[400][400];

ll Q;

ll l[MAX], r[MAX];

ll cost[MAX];



int main(void) {

    ll i, j;

    cin >> H >> W >> D;

    map<ll, P> mp;

    for(i=0; i<H; i++) {

        for(j=0; j<W; j++) {

            cin >> a[i][j];

            mp[a[i][j]]=mkp(i, j);

        }

    }

    

    for(i=1; i<=D; i++) {

        for(j=i; j+D<=H*W; j+=D) {

            cost[j+D]=cost[j]+abs(mp[j].first-mp[j+D].first)+abs(mp[j].second-mp[j+D].second);

        }

    }

    

    

    cin >> Q;

    for(i=0; i<Q; i++) {

        cin >> l[i] >> r[i];

    }

    

    for(i=0; i<Q; i++) {

        pr(cost[r[i]]-cost[l[i]]);

    }

    

    

    

}






































// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

#define rep(i, n) for(int i = 0; i < n; i++)

using namespace std;

typedef long long ll;

ll mod = 1000000007;



int main() {

    int h, w, d, a, q, l, r;

    int X[90010], Y[90010], dist[90010] = {};

    cin >> h >> w >> d;

    rep(i, h) {

        rep(j, w) {

            cin >> a;

            X[a] = i;

            Y[a] = j;

        }    

    }



    for(int i = d+1; i <= h*w; i++) {

        dist[i] = dist[i-d] + abs(X[i] - X[i-d]) + abs(Y[i] - Y[i-d]);

    }



    cin >> q;

    while(q--) {

        cin >> l >> r;

        cout << dist[r] - dist[l] << "\n";

    }



    return 0;

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <map>

using namespace std;

typedef long long ll;

typedef vector<ll> vl;

typedef vector<vector<ll>> vvl;

#define rep(i, n) for(ll i = 0; i < n; i++)

#define exrep(i, a, b) for(ll i = a; i <= b; i++)

#define out(x) cout << x << endl

#define exout(x) printf("%.10f\n", x)

#define chmax(x, y) x = max(x, y)

#define chmin(x, y) x = min(x, y)

#define all(a) a.begin(), a.end()

#define rall(a) a.rbegin(), a.rend()

#define pb push_back

#define re0 return 0

const ll mod = 1000000007;

const ll INF = 1e16;

const ll MAX_N = 100010;



ll manhattan(ll x1, ll y1, ll x2, ll y2) {  // (x1, y1) と (x2, y2) のマンハッタン距離

    return abs(x1 - x2) + abs(y1 - y2);

}



int main() {

    ll h, w, d;

    cin >> h >> w >> d;



    vl px(h*w+1), py(h*w+1);

    rep(i, h) {

        rep(j, w) {

            ll a;

            cin >> a;

            px[a] = i, py[a] = j;

        }

    }



    vl dist(h*w+1);

    rep(i, h*w+1) {

        if(i > d) {

            dist[i] = dist[i-d] + manhattan(px[i], py[i], px[i-d], py[i-d]);

        }

    }



    ll q;

    cin >> q;

    rep(i, q) {

        ll L, R;

        cin >> L >> R;

        out(dist[R] - dist[L]);

    }



    re0;

}

// Fastest known solution for this problem: