using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;

namespace ABC089
{
    public class Program
    {
        struct Point
        {
            readonly int x;
            readonly int y;

            public Point(int x, int y)
            {
                this.x = x;
                this.y = y;
            }

            public int GetManhattanDistance(Point other)
            {
                return Math.Abs(x - other.x) + Math.Abs(y - other.y);
            }
        }

        void Solve(StreamScanner ss, StreamWriter sw)
        {
            //---------------------------------
            var H = ss.Next(Int);
            var W = ss.Next(Int);
            var D = ss.Next(Int);
            var A = ss.Next(Int, W, H);
            var Q = ss.Next(Int);
            var LR = ss.Next(Int, 2, Q).Select(a => new {L = a[0], R = a[1]});

            var ps = new Point[H * W];
            for (var i = 0; i < H; i++)
            {
                for (var j = 0; j < W; j++)
                {
                    ps[A[i][j] - 1] = new Point(j, i);
                }
            }

            var dist = new int[H * W - D];
            for (var i = 0; i < D; i++)
            {
                for (var j = i + D; j < H * W; j += D)
                {
                    dist[j - D] = ps[j - D].GetManhattanDistance(ps[j]);
                }
            }


            var acs = new long[H * W];
            for (var i = 0; i < D; i++)
            {
                for (var j = i + D; j < H * W; j += D)
                {
                    acs[j] = acs[j - D] + dist[j - D];
                }
            }

            foreach (var lr in LR)
            {
                sw.WriteLine(acs[lr.R - 1] - acs[lr.L - 1]);
            }
            //---------------------------------
        }

        static void Main()
        {
            var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput()));
            var sw = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false};
            new Program().Solve(ss, sw);
            sw.Flush();
        }

        static readonly Func<string, string> String = s => s;
        static readonly Func<string, int> Int = int.Parse;
        static readonly Func<string, long> Long = long.Parse;
        static readonly Func<string, double> Double = double.Parse;
    }

    public class StreamScanner
    {
        static readonly char[] Sep = {' '};
        readonly Queue<string> buffer = new Queue<string>();
        readonly TextReader textReader;

        public StreamScanner(TextReader textReader)
        {
            this.textReader = textReader;
        }

        public T Next<T>(Func<string, T> parser)
        {
            if (buffer.Count != 0) return parser(buffer.Dequeue());
            var nextStrings = textReader.ReadLine().Split(Sep, StringSplitOptions.RemoveEmptyEntries);
            foreach (var nextString in nextStrings) buffer.Enqueue(nextString);
            return Next(parser);
        }

        public T[] Next<T>(Func<string, T> parser, int x)
        {
            var ret = new T[x];
            for (var i = 0; i < x; ++i) ret[i] = Next(parser);
            return ret;
        }

        public T[][] Next<T>(Func<string, T> parser, int x, int y)
        {
            var ret = new T[y][];
            for (var i = 0; i < y; ++i) ret[i] = Next(parser, x);
            return ret;
        }
    }
}

Measured memory_bytes: 26172