#include<bits/stdc++.h>
        #define ll long long
        #define f   first
        #define s   second
        #define pb          push_back
        #define mod        1000000007
        #define mod1       1000000009
        #define hell        998244353
        #define inf         1000000000000000000LL
        #define pi          3.14159265358979323
        #define N           1000050
        using namespace std;
        ll n,k,sum,q,res,m,val;
        ll x,y;
        void solve()    
        {   
            
            cin>>n>>m>>k;
            vector<int> par(n*m+1);
            for(int i=0;i<=n*m;i++) par[i]=i;
            int a[n][m];
            map<int,pair<int,int>> hm;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    cin>>a[i][j];
                    hm[a[i][j]]={i,j};
                }
            }
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if(hm.count(a[i][j]-k))
                    {
                        par[a[i][j]]=a[i][j]-k;
                    }
                }
            }
            vector<int> adj[n*m+1];
            bool vis[n*m+1]={0};
            for(int i=n*m;i>0;i--)
            {
                if(!vis[i] and par[i]!=i)
                {   int x=i;
                    while(par[x]!=x)
                    {
                        x=par[x];
                    }
                    for(int j=x;j<=i;j+=k)
                    {
                        vis[j]=1;
                        par[j]=x;
                        adj[x].pb(j);
                    }
                    //cout<<x<<" ";
                }

            }
            vector<int> adj2[n*m+1];
            for(int i=0;i<=n*m;i++)
            {
                if(adj[i].size()!=0)
                {
                    adj2[i].pb(0);
                    int n1=adj[i].size();
                    for(int j=0;j<n1-1;j++)
                    {
                        int x=adj[i][j];
                        int y=adj[i][j+1];

                        int z=abs(hm[x].f-hm[y].f)+abs(hm[x].s-hm[y].s);
                        //cout<<x<<y<<z<<" ";
                        adj2[i].pb(z);
                    }
                    for(int j=1;j<n1;j++)
                        adj2[i][j]+=adj2[i][j-1];
                    /*for(int j=0;j<n1;j++)
                        cout<<adj[i][j]<<" ";
                    cout<<"\n"; */
                }
            }
            cin>>q;
            while(q--)
            {
                int l,r;
                cin>>l>>r;
                if(l==r) {cout<<"0\n";continue;}
                int x=par[l];

                int id1=lower_bound(adj[x].begin(), adj[x].end(),l)-adj[x].begin();
                int id2=upper_bound(adj[x].begin(),adj[x].end(),r)-adj[x].begin()-1;
                cout<<adj2[x][id2]-adj2[x][id1]<<"\n";
            }





        }
        int main()
        {   ios_base::sync_with_stdio(false);
            cin.tie(0);cout.tie(0);
            ll t=1;
           //cin>>t;
            for(int i=1;i<=t;i++)
                solve();
            

        }            

Measured memory_bytes: 12664