#pragma GCC positionimize(3,"Otrst","inline")
//#include "emplace.hpp"
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=1e7+10;
const int INF=0x3f3f3f;
const double pi = 3.141592653;
const double E = 2.71828182846;
const ll mod=1e9+7;
const int M=1e3+5;
const double eps=1e-8;
#define ci(x) scanf("%d",&x)
#define co(x) printf("%d\n",x)
inline int read()
{
    int f=1,x=0;
    char c=getchar();
    while(c<'0' || c>'9')
    {
        if(c=='-')
            f=-1;
        c=getchar();
    }
    while(c>='0' && c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
    return f*x;
}
int prime(ll num)
{
    if(num == 1)
        return 0;
    if(num ==2|| num==3 )
        return 1 ;
    if(num %6!= 1&&num %6!= 5)
        return 0 ;
    int tmp =sqrt( num);
    for(int i= 5; i <=tmp; i+=6 )
        if(num %i== 0||num %(i+ 2)==0 )
            return 0 ;
    return 1 ;

}
int h,w,d,len;
int a[305][305];
struct node
{
    int x;
    int y;
} p[N];
int res[N];
int q;
void init()
{
    len=h*w;
    int l,r,sum;
    for(int i=1; i<=d; i++)
    {
        l=i;
        r=len;
        sum=0;
        res[i]=0;
        while( l <= r-d)
        {
            sum+=abs(p[l].x-p[l+d].x)+abs(p[l].y-p[l+d].y);
            l = l+d;
            res[l]=sum;
        }

    }
}
int main()
{
    ci(h);
    ci(w);
    ci(d);
    ci(q);
    for(int i=1; i<=h; i++)
        for(int j=1; j<=w; j++)
        {
            ci(a[i][j]);
            p[a[i][j]].x=i;
            p[a[i][j]].y=j;
        }
    init();
    while(q--)
    {
        int l,r;
        ci(l);
        ci(r);
        co(res[r]-res[l]);
    }
    return 0;
}
