We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).

The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.

You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.

You now have to take Q practical tests of your ability as a magical girl.

The i-th test will be conducted as follows:

* Initially, a piece is placed on the square where the integer L_i is written.

* Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.

* Here, it is guaranteed that R_i-L_i is a multiple of D.




For each test, find the sum of magic points consumed during that test.

Constraints

* 1 \leq H,W \leq 300
* 1 \leq D \leq H×W
* 1 \leq A_{i,j} \leq H×W
* A_{i,j} \neq A_{x,y} ((i,j) \neq (x,y))
* 1 \leq Q \leq 10^5
* 1 \leq L_i \leq R_i \leq H×W
* (R_i-L_i) is a multiple of D.

Input

Input is given from Standard Input in the following format:


H W D
A_{1,1} A_{1,2} ... A_{1,W}
:
A_{H,1} A_{H,2} ... A_{H,W}
Q
L_1 R_1
:
L_Q R_Q


Output

For each test, print the sum of magic points consumed during that test.

Output should be in the order the tests are conducted.

Examples

Input

3 3 2
1 4 3
2 5 7
8 9 6
1
4 8


Output

5


Input

4 2 3
3 7
1 4
5 2
6 8
2
2 2
2 2


Output

0
0


Input

5 5 4
13 25 7 15 17
16 22 20 2 9
14 11 12 1 19
10 6 23 8 18
3 21 5 24 4
3
13 13
2 10
13 13


Output

0
5
0

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static java.lang.Math.*;
import static java.lang.String.format;

public class Main {
    public static void main(String[] args) {
        solve();
    }

    final static int INF = Integer.MAX_VALUE >> 2;
    final static int MOD = 1_000_000_007;
    final static int[] dx4 = {0, 1, 0, -1};
    final static int[] dy4 = {1, 0, -1, 0};
    final static int[] dx9 = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
    final static int[] dy9 = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
    final static Runnable me = () -> {
    };

    public static void solve() {
        //TODO:Solve problem like ***
        final Scanner sc = new Scanner();
        int h = sc.nextInt();
        int w = sc.nextInt();
        int d = sc.nextInt();
        int[][] a = new int[h][w];
        int[] px = new int[h * w + 1];
        int[] py = new int[h * w + 1];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                a[i][j] = sc.nextInt();
                px[a[i][j]] = i;
                py[a[i][j]] = j;
            }
        }
        int[] rui = new int[h * w+1];
        for (int i = 1; i <= d; i++) {
            rui[i] = dis(px[1], py[1], px[i], py[i]);
        }
        for (int i = d + 1; i <= h * w; i++) {
            rui[i] = rui[i - d] + dis(px[i - d], py[i - d], px[i], py[i]);
        }
        StringBuilder sb = new StringBuilder();
        int q = sc.nextInt();
        for (int i = 0; i < q; i++) {
            int l= sc.nextInt();
            int r= sc.nextInt();
            sb.append(rui[r] - rui[l]);
            sb.append("\n");
        }
        print(sb);

    }
    private static int dis(FixedIntPair fip1,FixedIntPair fip2){
        return abs(fip1.x - fip2.x) + abs(fip1.y - fip2.y);
    }

    private static int dis(int x1, int y1, int x2, int y2) {
        return abs(x1 - x2) + abs(y1 - y2);
    }
    static class Encoder {
        final int mod;

        public Encoder(int mod) {
            this.mod = mod;
        }

        public int encode(int x, int y) {
            assert y < mod;
            return x * mod + y;
        }

        public int decodeX(int z) {
            return z / mod;
        }

        public int decodeY(int z) {
            return z % mod;
        }
    }

    static class Accepter<T> {
        private T val;
        private final Predicate p;

        public Accepter(T defaultValue, Predicate p) {
            this.val = defaultValue;
            this.p = p;
        }

        /**
         * @return accepted newval?
         */
        public boolean replace(T newVal) {
            if (p.test(newVal)) {
                this.val = newVal;
                return true;
            }
            return false;
        }
    }

    //runWhenEAで使う
    private static Runnable func(Object... objects) {
        try {
            assert false;
            return me;
        } catch (AssertionError e) {
            return () -> {
                put(objects);
            };
        }
    }

    private static void print(Object... objects) {
        if (objects.length == 1) {
            System.out.print(objects[0]);
        } else {
            System.out.print(Arrays.toString(objects));
        }
    }

    private static void put(Object... objects) {
        print(objects);
        put();
    }

    private static void put() {
        System.out.println();
    }


    private static void runWhenEA(Runnable runnable) {
        try {
            assert false;
        } catch (AssertionError e) {
            PrintStream ps = System.out;
            PrintStream pse = System.err;
            System.setOut(pse);
            runnable.run();
            System.setOut(ps);
        }
    }

    private static void putM(String name, char[][] mat) {
        put("---------------------" + name + "-----------------");
        for (int i = 0; i < mat.length; i++) {
            put(Arrays.toString(mat[i]));
        }
    }

    private static void putM(String name, int[][] mat) {
        put("---------------------" + name + "-----------------");
        for (int i = 0; i < mat.length; i++) {
            put(Arrays.toString(mat[i]));
        }
    }

    private static void putM(String name, long[][] mat) {
        put("---------------------" + name + "-----------------");
        for (int i = 0; i < mat.length; i++) {
            put(Arrays.toString(mat[i]));
        }
    }

    private static void putM(String name, boolean[][] mat) {
        put("---------------------" + name + "-----------------");
        for (int i = 0; i < mat.length; i++) {
            put(Arrays.toString(mat[i]));
        }
    }

    private static final class Scanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;

        private boolean hasNextByte() {
            if (ptr < buflen) {
                return true;
            } else {
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (buflen <= 0) {
                    return false;
                }
            }
            return true;
        }

        private int readByte() {
            if (hasNextByte())
                return buffer[ptr++];
            else
                return -1;
        }

        private boolean isPrintableChar(int c) {
            return 33 <= c && c <= 126;
        }

        public boolean hasNext() {
            while (hasNextByte() && !isPrintableChar(buffer[ptr]))
                ptr++;
            return hasNextByte();
        }

        public String next() {
            if (!hasNext())
                throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while (isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public long nextLong() {
            if (!hasNext())
                throw new NoSuchElementException();
            long n = 0;
            boolean minus = false;
            int b = readByte();
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            if (b < '0' || '9' < b) {
                throw new NumberFormatException();
            }
            while (true) {
                if ('0' <= b && b <= '9') {
                    n *= 10;
                    n += b - '0';
                } else if (b == -1 || !isPrintableChar(b)) {
                    return minus ? -n : n;
                } else {
                    throw new NumberFormatException();
                }
                b = readByte();
            }
        }

        public int nextInt() {
            long nl = nextLong();
            if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
                throw new NumberFormatException();
            return (int) nl;
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    private static class FixedIntPair {
        public final int x, y;
        public static final FixedIntPair ZEROS = new FixedIntPair(0, 0);

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

        @Override
        public String toString() {
            return format(FixedIntPair.class.getSimpleName() + ":(%d,%d)", x, y);
        }
    }

    private static final class FixedLongPair {
        public final long x, y;
        public static final FixedLongPair ZEROS = new FixedLongPair(0, 0);

        FixedLongPair(long x, long y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int hashCode() {
            return (int) x + (int) y;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) return false;
            if (!(obj instanceof FixedLongPair)) return false;
            FixedLongPair pair = (FixedLongPair) obj;
            return this.x == pair.x && this.y == pair.y;
        }

        @Override
        public String toString() {
            return format(FixedLongPair.class.getSimpleName() + ":(%d,%d)", x, y);
        }
    }

    private static final class Binary {
        public static String toZeroPadding(int i) {
            return format("%" + Integer.toBinaryString(-1).length() + "s", Integer.toBinaryString(i)).replace(' ', '0');
        }

        public static String toZeroPadding(long i) {
            return format("%" + Long.toBinaryString(-1).length() + "s", Long.toBinaryString(i)).replace(' ', '0');
        }
    }

    public static class BinaryIndexedTree {
        final int n;
        final long[] array;

        BinaryIndexedTree(long[] array) {
            this.n = array.length;
            this.array = new long[n];
            for (int i = 0; i < array.length; i++) {
                add(i, array[i]);
            }
        }

        BinaryIndexedTree(int[] array) {
            this.n = array.length;
            this.array = new long[n];
            for (int i = 0; i < array.length; i++) {
                add(i, array[i]);
            }
        }

        public void add(int index, long val) {
            for (; index < n; index |= index + 1) {
                array[index] += val;
            }
        }

        public long sum(int index) {
            long result = 0;
            for (; index >= 0; index = (index & (index + 1)) - 1) {
                result += array[index];
            }
            return result;
        }

        /**
         * @param array arrayが1～nの順列になっていないならば定義されない
         * @return 転倒数
         */
        public static int inv(int[] array) {
            int[] a = new int[array.length];
            BinaryIndexedTree bit = new BinaryIndexedTree(a);
            bit.add(array[0] - 1, 1);
            int result = 0;
            for (int i = 1; i < array.length; i++) {
                System.out.println(bit);
                result += i - bit.sum(array[i] - 1);
                bit.add(array[i] - 1, 1);
            }
            return result;
        }

        @Override
        public String toString() {
            long[] array = new long[n];
            for (int i = 0; i < n; i++) {
                array[i] = sum(i);
            }
            String result = "";
            result += "ruiseki:" + Arrays.toString(array) + "\n";
            array[0] = sum(0);
            for (int i = 1; i < n; i++) {
                array[i] = sum(i) - sum(i - 1);
            }
            result += "source::" + Arrays.toString(array);
            return result;
        }
    }

    private static final class Util {
        static long gcd(long a, long b) {
            a = abs(a);
            b = abs(b);
            if (a < b) {
                long tmp = a;
                a = b;
                b = tmp;
            }
            while (b != 0) {
                long r = a % b;
                a = b;
                b = r;
            }
            return a;
        }

        static int gcd(int a, int b) {
            a = abs(a);
            b = abs(b);
            if (a < b) {
                int tmp = a;
                a = b;
                b = tmp;
            }
            while (b != 0) {
                int r = a % b;
                a = b;
                b = r;
            }
            return a;
        }

        static long lcm(long a, long b) {
            long gcd = gcd(a, b);
            long result = b / gcd;
            return a * result;
        }

        static boolean isValidCell(int i, int j, int h, int w) {
            return i >= 0 && i < h && j >= 0 && j < w;
        }
    }
}































































/*
......................................................................................................................................
.....................................  ________ ____ __________________________________________ .....................................
..................................... /  _____/|    |   \      \__    ___/\_   _____/\______   \.....................................
...................................../   \  ___|    |   /   |   \|    |    |    __)_  |       _/.....................................
.....................................\    \_\  \    |  /    |    \    |    |        \ |    |   \.....................................
..................................... \______  /______/\____|__  /____|   /_______  / |____|_  /.....................................
.....................................        \/                \/                 \/         \/ .....................................
......................................................................................................................................
.............................................................,;'';:...................................................................
........................................................+@@@@@@@@@@@@@@'..............................................................
.....................................................#@@@##############@@@:...........................................................
...................................................@@@####################@@,.........................................................
.................................................@@#########################@@........................................................
...............................................:@############################@@.......................................................
..............................................@@######@@@#';;'#@@@############@@:.....................................................
.............................................@#####@@,````````````,@@###########@:....................................................
............................................@####@;``````````````````+@##########@....................................................
...........................................@###@:``````````````````````#@########@@...................................................
..........................................@####``````````````````````````@########@@..................................................
.........................................###@.````````````````````````````@########@+.................................................
.........................................@#@```````````````````````````````#########@.................................................
........................................@#@`````````````````````````````````########@@................................................
.......................................,@@```````````````````````````````````@#######@:...............................................
.......................................@@`````````````````````````````````````@#######@...............................................
.......................................@:````````````````````#@@'``````````````@######@+..............................................
......................................#@```````````````````@@@@@@@#````````````########@..............................................
......................................@```````````````````@@@@@@@@@@````````````@######@+.............................................
......................................@``````````````````@@@@@@@+   +```````````+#######@.............................................
.....................................;:``````````````````@@@@@@@    @````````````@######@'............................................
.....................................@``````````````````:@@@@@@@    @````````````@#######@............................................
.....................................@```,@@@#``````````;@@@@@@@    @````````````:#######@:...........................................
.....................................@``@@@@@@@@````````.@@@@@@@#  ,#`````````````@#######@...........................................
.....................................@`@@@@@@@+'@````````@@@@@@@@@@@``````````````@#######@...........................................
.....................................@,@@@@@@   ,```:+:``:@@@@@@@@@.``````````````@########@..........................................
.....................................#@@@@@@@  ;@@#;,,,@``:@@@@@@@````````````````#########@..........................................
.....................................+@@@@@@@@',,,,,,,,;,```.'+;``````````````````'########@;.........................................
.....................................'@@@@',,,,,,,,,,,,,@`````````````````````````:#########@.........................................
....................................:@#,,,,,:;;;;;:,,,,,@`````````````````````````.#########@.........................................
.................................:@#@@@@#++';;;;;;;;;;;;@``````````````````````````##########+........................................
...............................#@#+;;;;;;;;;;;;;;;;;;;;':``````````````````````````##########@........................................
....................................,@#@@@@@#+'';;;;;+@#```````````````````````````##########@........................................
.....................................@``````````.,,,.``````````````````````````````############.......................................
.....................................@`````````````````````````````````````````````#######+'+#@.......................................
.....................................@`````````````````````````````````````````````##########'@.......................................
.....................................#`````````````````````````````````````````````############@#.....................................
.....................................:.````````````````````````````````````````````##############@,...................................
......................................+```````````````````````````````````````````.###############@#..................................
......................................@```````````````````````````````````````````.################@@.................................
......................................@```````````````````````````````````````````.###+##############@................................
......................................@```````````````````````````````````````````.###+###############@...............................
......................................',``````````````````````````````````````````.####'##############@@..............................
.......................................@```````````````````````````````````````````#####+##############@:.............................
.......................................@```````````````````````````````````````````#####'###############@.............................
.......................................@```````````````````````````````````````````######'################............................
.......................................#,``````````````````````````````````````````#######'##############@............................
........................................@``````````````````````````````````````````@######++##############+...........................
........................................@``````````````````````````````````````````@#######'##############@...........................
........................................@``````````````````````````````````````````@########'#############@...........................
.......................................@#'`````````````````````````````````````````@#########'##############..........................
.......................................@#@`````````````````````````````````````````+#########+'############@..........................
......................................@##@`````````````````````````````````````````.##########+'###########@..........................
......................................@##@:`````````````````````````````````````````###########+'###########..........................
.....................................:@###@`````````````````````````````````````````@###########+'+#########,.........................
.....................................@####@`````````````````````````````````````````@#############''########..........................
.....................................@####@.````````````````````````````````````````;##############+'######@..........................
.....................................@#####@`````````````````````````````````````````################@@@###+..........................
.....................................@#####@`````````````````````````````````````````@###############@..;;............................
....................................,@#####@.````````````````````````````````````````+################'...............................
....................................:#######@`````````````````````````````````````````################@...............................
....................................:#######@`````````````````````````````````````````@###############@...............................
....................................,@#######,````````````````````````````````````````:###############@...............................
.....................................@######@@`````````````````````````````````````````@##############@...............................
.....................................@######@@`````````````````````````````````````````+##############@...............................
.....................................@#####@,;;`````````````````````````````````````````@#############@...............................
.....................................@####@@..@`````````````````````````````````````````+#############@...............................
.....................................,####@...@``````````````````````````````````````````@############+...............................
......................................@##@.....@`````````````````````````````````````````:###########@,...............................
.......................................@+......@``````````````````````````````````````````@##########@................................
...............................................:#``````````````````````````````````````````##########@................................
................................................@``````````````````````````````````````````+########@,................................
................................................'+``````````````````````````````````````````@#######@.................................
.................................................@```````````````````````````````````````````@#####@:.................................
.................................................'#``````````````````````````````````````````.#####@..................................
..................................................@```````````````````````````````````````````;###@...................................
...................................................@```````````````````````````````````````````+#@'...................................
...................................................'#```````````````````````````````````````````@#....................................
....................................................##`````````````````````````````````````````@#.....................................
.....................................................#@```````````````````````````````````````@+......................................
......................................................:@;```````````````````````````````````;@,.......................................
.......................................................;@@'```````````````````````````````:@@+;.......................................
.......................................................@,,'@@'``````````````````````````@@@,,,@.......................................
......................................................@,,,,,,'@@@@;````````````````.+@@@;,,,,,@.......................................
......................................................#@+@,,,,,,,,+@@@@@@@@@@@@@@@@@;,,,,,'@@@........................................
.........................................................+,,,#',,@@..............@,,,,,,,,@...........................................
..........................................................@@@,#@@,...............:+,,,'@,,@...........................................
..................................................................................@,,,@.##............................................
...................................................................................@;@................................................
....................................................................................:.................................................
......................................................................................................................................
......................................................................................................................................
 */
