stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std ;



int main()

{

	int n ;

	while( cin >> n ,n )

	{

		int m ;

		cin >> m ;

		int num[ n + 1 ] ;

		num[ 0 ] = 0 ;

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

		{

			cin >> num[ i ] ;

			num[ i ] += num[ i - 1 ] ;

		}

		int ans = 0 ;

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

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

		cout << ans << endl ;

	}

	return 0 ;

}

// Faster C++ solution for the same problem (measured speedup: 5.021657800344684x):
#include<stdio.h>



#include<algorithm>





using namespace std ;







int main()

{

	int n ,k ;



	while( 1 )

	{

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





		if( !( n | k ) )

		{

			return 0 ;

		}





		int if_k = k - 1 ;



		int ans = -10001 ,other[ 100000 ] ;

		

		int sum = 0 ;



		int i ;

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

		{

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



			sum += other[ i ] ;





			if( i > if_k )

			{

				sum -= other[ i - k ] ;

			}



			if( i >= if_k )

			{

				ans = max( ans ,sum ) ;

			}

		}





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

	}

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 2.577273743600113x):
#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;

    }

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.0548059429052x):
#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;

}
// Slower C++ solution:
#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;

	}

}

// Faster C++ solution for the same problem (measured speedup: 1.8860419500475842x):
#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;

	}

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.886151576960251x):
#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;

	}

}
// Slower C++ solution:
#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;

  }

}

// Faster C++ solution for the same problem (measured speedup: 3.8569944274590275x):
#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);

  }

}
// Slower C++ solution:
#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);

    }

}

// Faster C++ solution for the same problem (measured speedup: 1.1042737538631018x):
#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);

	}

}
// Slower C++ solution:
#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;

    }

}

// Faster C++ solution for the same problem (measured speedup: 3.18373962909723x):
#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;

    }

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.351616217642816x):
#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;

}
// Slower C++ solution:
#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;

  }

}


// Faster C++ solution for the same problem (measured speedup: 1.1601614465222458x):
// 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;

  }

}
// Slower C++ solution:
//   _/                _/                 _/_/_/   _/

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

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

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

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

#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;

  }

}

// Faster C++ solution for the same problem (measured speedup: 1.2890159154344016x):
/*   _/                _/                 _/_/_/   _/

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

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

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

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

#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";

  }

}
// Slower C++ solution:
#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;

	

}


// Faster C++ solution for the same problem (measured speedup: 1.3548354022098488x):
#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;

	

}
// Slower C++ solution:
#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;

    }

}


// Faster C++ solution for the same problem (measured speedup: 2.44886114445758x):
#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]]);

    }

    

    

    

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.3592548321975133x):
#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;

}