using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using static System.Math;
using static System.Array;
using static AtCoderSolve.Tool;

namespace AtCoderSolve
{
    class Solve
    {
        const int mod = 1000000007;
        const int INF = int.MaxValue / 2;
        const long SINF = long.MaxValue / 2;


        static void Main(string[] args)
        {
            //var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            //Console.SetOut(sw);
            var cin = new Scanner();

            int H, W, D;
            var p = cin.SplitReader();
            H = p[0];
            W = p[1];
            D = p[2];
            int[][] A = new int[H * W + 1][];

            for(var i = 0; i < H; i++)
            {
                var input = cin.SplitReader();
                for(var j = 0; j < W; j++)
                {
                    A[input[j]] = new int[2];
                    A[input[j]][0] = i + 1;
                    A[input[j]][1] = j + 1;
                }
            }

            int Q = int.Parse(Console.ReadLine());
            int[] L = new int[Q];
            int[] R = new int[Q];
            for(var i = 0; i < Q; i++)
            {
                var lr = cin.SplitReader();
                L[i] = lr[0];
                R[i] = lr[1];
            }


            List<List<long>> c = new List<List<long>>();
            for(var r = 1; r <= D; r++)
            {
                c.Add(new List<long>());
                int k = 0;
                
                while (true)
                {
                    int cur = r + k * D;
                    if (cur > H * W)
                    {
                        break;
                    }
                    if (cur == r) 
                    {
                        c[r - 1].Add(0);
                    }
                    else
                    {
                        c[r - 1].Add(c[r - 1][k - 1] + Abs(A[cur][0] - A[cur - D][0]) + Abs(A[cur][1] - A[cur - D][1]));
                    }
                    k++;
                }
            }

            for(var i = 0; i < Q; i++)
            {
                int r = L[i] % D == 0 ? D : L[i] % D;
                Console.WriteLine(c[r - 1][(R[i] - r) / D] - c[r - 1][(L[i] - r) / D]);
            }

            //Console.Out.Flush();
        }

    }

    public class Scanner
    {
        public int[] SplitReader()
        {
            int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return array;
        }
        public long[] SplitReaderL()
        {
            long[] array = Console.ReadLine().Split().Select(long.Parse).ToArray();
            return array;
        }
    }

    public static class Tool
    {
        static public void Initialize<T>(ref T[] array, T initialvalue)
        {
            for(var i = 0; i < array.Length; i++)
            {
                array[i] = initialvalue;
            }
        }
        static public long ModuloP(long value,long mod)
        {
            long r = value % mod;
            
            if (r * mod < 0)
            {
                r += mod;
            }
            return r;
        }

    }

    struct Pair<T> : IComparable<Pair<T>> where T : IComparable<T>
    {
        public T variable;
        public int index;

        public Pair(T val, int i)
        {
            variable = val;
            index = i;
        }

        public int CompareTo(Pair<T> otherPair)
        {
            return variable.CompareTo(otherPair.variable);
        }

        public int CompareIndex(Pair<T> otherPair)
        {
            return index.CompareTo(otherPair.index);
        }
    }

    

}






Measured memory_bytes: 32016