It is the hard version of the problem. The only difference is that in this version 1 ≤ n ≤ 300.

In the cinema seats can be represented as the table with n rows and m columns. The rows are numbered with integers from 1 to n. The seats in each row are numbered with consecutive integers from left to right: in the k-th row from m (k - 1) + 1 to m k for all rows 1 ≤ k ≤ n.

1| 2| ⋅⋅⋅| m - 1| m  
---|---|---|---|---  
m + 1| m + 2| ⋅⋅⋅| 2 m - 1| 2 m  
2m + 1| 2m + 2| ⋅⋅⋅| 3 m - 1| 3 m  
\vdots| \vdots| \ddots| \vdots| \vdots  
m (n - 1) + 1| m (n - 1) + 2| ⋅⋅⋅| n m - 1| n m  
The table with seats indices

There are nm people who want to go to the cinema to watch a new film. They are numbered with integers from 1 to nm. You should give exactly one seat to each person.

It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. i-th person has the level of sight a_i. Let's define s_i as the seat index, that will be given to i-th person. You want to give better places for people with lower sight levels, so for any two people i, j such that a_i < a_j it should be satisfied that s_i < s_j.

After you will give seats to all people they will start coming to their seats. In the order from 1 to nm, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through.

Let's consider an example: m = 5, the person has the seat 4 in the first row, the seats 1, 3, 5 in the first row are already occupied, the seats 2 and 4 are free. The inconvenience of this person will be 2, because he will go through occupied seats 1 and 3.

Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied).

Input

The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 100) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 300) — the number of rows and places in each row respectively.

The second line of each test case contains n ⋅ m integers a_1, a_2, …, a_{n ⋅ m} (1 ≤ a_i ≤ 10^9), where a_i is the sight level of i-th person.

It's guaranteed that the sum of n ⋅ m over all test cases does not exceed 10^5.

Output

For each test case print a single integer — the minimal total inconvenience that can be achieved.

Example

Input


7
1 2
1 2
3 2
1 1 2 2 3 3
3 3
3 4 4 1 1 1 1 1 2
2 2
1 1 2 1
4 2
50 50 50 50 3 50 50 50
4 2
6 6 6 6 2 2 9 6
2 9
1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3


Output


1
0
4
0
0
0
1

Note

In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is 1.

In the second test case the optimal seating looks like this: 

<image>

In the third test case the optimal seating looks like this: 

<image>

The number in a cell is the person's index that sits on this place.
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    // final CustomReader in = new CustomReader("input.txt");
    final CustomReader in = new CustomReader();
    final PrintWriter out = new PrintWriter(System.out);

    void solve() {
        int t = in.nextInt();
        while (t-- > 0) {
            int n = in.nextInt();
            int m = in.nextInt();

            Person[] arr = new Person[n*m];
            for (int i = 0; i < n*m; i++) {
                int sight = in.nextInt();
                Person person = new Person();
                person.id = i;
                person.sight = sight;
                arr[i] = person;
            }

            Arrays.sort(arr, (p1, p2) -> {
                if (p1.sight == p2.sight) return p1.id - p2.id;
                return p1.sight - p2.sight;
            });

            for (int i = 0, j=0; i < n*m; ) {
                while (j < n*m && arr[j].sight == arr[i].sight) j++;
                int maxSeat = j - 1;
                int minSeat = i;

                int minRow = minSeat / m;
                int minCol = minSeat % m;
                int maxRow = maxSeat / m;
                int maxCol = maxSeat % m;

                boolean lol = false;

                if (minRow < maxRow) {
                    lol = true;
                    for (int col = m-1; col >= minCol; col--) {
                        arr[i].seatRow = minRow;
                        arr[i].seatCol = col;
                        i++;
                    }
                    minRow++;
                }

                while (minRow < maxRow) {
                    for (int col = m-1; col >= 0; col--) {
                        arr[i].seatRow = minRow;
                        arr[i].seatCol = col;
                        i++;
                    }
                    minRow++;
                }

                if (lol) {
                    for (int col = maxCol; col >= 0; col--) {
                        arr[i].seatRow = minRow;
                        arr[i].seatCol = col;
                        i++;
                    }
                } else {
                    for (int col = maxCol; col >= minCol; col--) {
                        arr[i].seatRow = minRow;
                        arr[i].seatCol = col;
                        i++;
                    }
                }
            }

            Arrays.sort(arr, Comparator.comparingInt(p -> p.id));

            long answer = 0;
            SegmentTree[] segmentTrees = new SegmentTree[n];
            for (int i = 0; i < n; i++) segmentTrees[i] = new SegmentTree(new int[m]);
            for (int i = 0; i < n*m; i++) {
                answer += segmentTrees[arr[i].seatRow].query(0, arr[i].seatCol);
                segmentTrees[arr[i].seatRow].set(arr[i].seatCol, 1);
            }
            out.println(answer);
        }
    }

    class Person {
        int id;
        int sight;
        int seat;
        int seatRow;
        int seatCol;
    }

    public class SegmentTree {

        /* *********************************** */
        /* CHANGE HERE FOR OTHER FUNCTIONALITY */

        int getLeafValue(int arrayValue) {
            return arrayValue;
        }

        int combineValues(int value1, int value2) {
            return value1 + value2;
        }

        /* CHANGE HERE FOR OTHER FUNCTIONALITY */
        /* *********************************** */

        private static final int NULL = 0;
        private final int[] tree;

        public SegmentTree(int[] arr) {
            int padLen = 1;
            while (padLen < arr.length) padLen <<= 1;
            tree = new int[2*padLen];
            for (int i = 0; i < padLen; i++) {
                if (i < arr.length) tree[i+padLen] = getLeafValue(arr[i]);
                else tree[i+padLen] = NULL;
            }
            while (padLen/2 > 0) {
                for (int i = padLen/2; i < padLen; i++) {
                    if (tree[2*i+1] == NULL) tree[i] = tree[2*i];
                    else tree[i] = combineValues(tree[2*i], tree[2*i+1]);
                }
                padLen /= 2;
            }
        }

        public int query(int left, int right) {
            left = tree.length/2  + left;
            right = tree.length/2 + right;
            int result = tree[left];
            if (left == right) return result;
            result = combineValues(result, tree[right]);
            while (left/2 != right/2) {
                if (left % 2 == 0) result = combineValues(result, tree[left+1]);
                if (right % 2 == 1) result = combineValues(result, tree[right-1]);
                left /= 2;
                right /= 2;
            }
            return result;
        }

        public void set(int index, int value) {
            index = tree.length/2 + index;
            tree[index] = getLeafValue(value);
            while (index/2 > 0) {
                index /= 2;
                if (tree[2*index+1] == NULL) tree[index] = tree[2*index];
                else tree[index] = combineValues(tree[2*index], tree[2*index+1]);
            }
        }
    }

    public static void main(String[] args) { Main s = new Main(); s.solve(); s.out.close(); }

    private static class CustomReader {
        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar;
        private int numChars;

        CustomReader() {
            stream = System.in;
        }

        CustomReader(String fileName) {
            InputStream temp = null;
            try {
                temp = new FileInputStream(fileName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            stream = temp;
        }

        String nextLine() {
            int c = read();
            StringBuilder res = new StringBuilder();
            while (!isEndOfLine(c)) {
                res.appendCodePoint(c);
                c = read();
            }
            if (c == '\r') read();
            return res.toString();
        }

        String nextString() {
            int c = read();
            while (isSpaceChar(c)) c = read();
            StringBuilder res = new StringBuilder();
            while (!isSpaceChar(c)) {
                res.appendCodePoint(c);
                c = read();
            }
            if (c == '\r') read();
            return res.toString();
        }

        long nextLong() {
            int c = read();
            while (isSpaceChar(c)) c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            while (!isSpaceChar(c)) {
                if (c < '0' || c > '9') throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '\r') read();
            return res * sgn;
        }

        int nextInt() {
            int c = read();
            while (isSpaceChar(c)) c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            while (!isSpaceChar(c)) {
                if (c < '0' || c > '9') throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '\r') read();
            return res * sgn;
        }

        int[] nextArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) arr[i] = nextInt();
            return arr;
        }

        double nextDouble() {
            return Double.parseDouble(nextString());
        }

        boolean hasNext() {
            if (numChars == -1) return false;
            return curChar < numChars || curChar == buf.length || numChars == 0; // Probably
        }

        private int read() {
            if (numChars == -1) throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) return -1;
            }
            return buf[curChar++];
        }

        private static boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private static boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }
    }
}
