description: 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.
solution: import java.util.*;
  import java.io.*;
  import java.lang.reflect.Array;
  
  public class Main{
  
      static int mod = (int) (Math.pow(10, 9)+7);
  	static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
  	static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 };
  	static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 };
  	static final int inf = Integer.MAX_VALUE / 2;
  	static final long infL = Long.MAX_VALUE / 3;
  	static final double infD = Double.MAX_VALUE / 3;
  	static final double eps = 1e-10;
  	static final double pi = Math.PI;
      static List<Integer> primeNumbers = new ArrayList<>();
      public static void main(String[] args) {
          MyScanner sc = new MyScanner();
          out = new PrintWriter(new BufferedOutputStream(System.out));
  
          int test= sc.nextInt();
  
          while(test -- > 0){
  
              int n = sc.nextInt();
              int m = sc.nextInt();
  
              int[] arr = new int[n*m];
              int[] sorted = new int[n*m];
  
              for(int i= 0; i < n*m; i++){
                  arr[i] = sc.nextInt();
                  sorted[i] = arr[i];
              }
  
              sort(sorted);
  
              int[][] dummy = new int[n][m];
  
              for(int i= 0; i < n*m ; i++){
                  int element = sorted[i];
                  dummy[i/m][i%m] = element;
              }
  
              //just store the points int 
              Map<Integer, ArrayList<Pair>> order = new HashMap<>();
              for(int i = 0; i < n; i++){
                  for(int j = m-1 ; j >= 0; j--){
                      int current = dummy[i][j];
                      order.putIfAbsent(current, new ArrayList<>());
                      var list = order.get(current);
                      list.add(new Pair(i,j));
                  }
              }
  
              int[][] empty = new int[n][m];
  
              int count = 0;
              for(int i= 0; i < n*m ; i++){
  
                  int element = arr[i];
                  var list = order.get(element);
                  Pair req = list.get(0);
                  list.remove(0);
                  order.put(element, list);
  
                  //now check the n and m
                  int row = req.a;
                  int col = req.b;
  
                  for(int j = 0; j < col;  j++){
                      if(empty[row][j] == 1) count++;
                  }
                  
                  empty[row][col] = 1;
              }
  
              out.println(count);
          }
  
          out.close();
      }
  
      //-------------------------------------------------------------------
      //-------------------------------------------------------------------
      //-------------------------------------------------------------------
      //-------------------------------------------------------------------
      //-------------------------------------------------------------------
  
      //prime sieve
      public static void primeSieve(int n){
          BitSet bitset = new BitSet(n+1);
          for(long i = 0; i < n ; i++){
              if (i == 0 || i == 1) {
                  bitset.set((int) i);
                  continue;
              }
              if(bitset.get((int) i)) continue;
              primeNumbers.add((int)i);
              for(long j = i; j <= n ; j+= i)
                  bitset.set((int)j);
          }
      }
  
      //number of divisors
      public static int countDivisors(long number){
          if(number == 1) return 1;
          List<Integer> primeFactors = new ArrayList<>();
          int index = 0;
          long curr = primeNumbers.get(index);
          while(curr * curr <= number){
              while(number % curr == 0){
                  number = number/curr;
                  primeFactors.add((int) curr);
              }    
              index++;
              curr = primeNumbers.get(index);
          }
  
          if(number != 1) primeFactors.add((int) number);
          int current = primeFactors.get(0);
          int totalDivisors = 1;
          int currentCount = 2;
          for (int i = 1; i < primeFactors.size(); i++) {
              if (primeFactors.get(i) == current) {
                  currentCount++;
              } else {
                  totalDivisors *= currentCount;
                  currentCount = 2;
                  current = primeFactors.get(i);
              }
          }
          totalDivisors *= currentCount;
          return totalDivisors;
      }
  
      //now adding next permutation function to java hehe
      public static boolean next_permutation(int[] p) {
          for (int a = p.length - 2; a >= 0; --a)
            if (p[a] < p[a + 1])
              for (int b = p.length - 1;; --b)
                if (p[b] > p[a]) {
                  int t = p[a];
                  p[a] = p[b];
                  p[b] = t;
                  for (++a, b = p.length - 1; a < b; ++a, --b) {
                    t = p[a];
                    p[a] = p[b];
                    p[b] = t;
                  }
                  return true;
                }
          return false;
      }
  
      //finding the value of NCR in O(RlogN) time and O(1) space
      public static long getNcR(int n, int r)
      {
          long p = 1, k = 1;
          if (n - r < r) r = n - r;
  
          if (r != 0) {
              while (r > 0) {
                  p *= n;
                  k *= r;
                  long m = __gcd(p, k);
                  p /= m;
                  k /= m;
                  n--;
                  r--;
              }
          }
          else {
              p = 1;
          }
          return p;
      }
   
      //for ncr calculator
      public static long __gcd(long n1, long n2)
      {
          long gcd = 1;
          for (int i = 1; i <= n1 && i <= n2; ++i) {
              // Checks if i is factor of both integers
              if (n1 % i == 0 && n2 % i == 0) {
                  gcd = i;
              }
          }
          return gcd;
      }
  
  
  
      //is vowel function 
      public static boolean isVowel(char c)
      {
          return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' ||     c=='u' || c=='U');
      }   
  
      //two add two big numbers with some mod 
      public static int add(int a, int b) {
  		a+=b;
  		if (a>=mod) return a-mod;
  		return a;
  	}
  	
      //two sub two numbers
  	public static int sub(int a, int b) {
  		a-=b;
  		if (a<0) a+=mod;
  		else if (a>=mod) a-=mod;
  		return a;
  	}
   
      //to sort the array with better method
  	public static void sort(int[] a) {
  		ArrayList<Integer> l=new ArrayList<>();
  		for (int i:a) l.add(i);
  		Collections.sort(l);
  		for (int i=0; i<a.length; i++) a[i]=l.get(i);
  	}
  	
      //for calculating binomialCoeff
      public static int binomialCoeff(int n, int k)
      {
          int C[] = new int[k + 1];
          // nC0 is 1
          C[0] = 1;
          for (int i = 1; i <= n; i++) {
              // Compute next row of pascal
              // triangle using the previous row
              for (int j = Math.min(i, k); j > 0; j--)
                  C[j] = C[j] + C[j - 1];
          }
          return C[k];
      }
  
      //Pair with int int 
      public static class Pair{
  
          public int a;
          public int b;
  
          Pair(int a , int b){
              this.a = a;
              this.b = b;
          }
  
          @Override
          public String toString(){
              return a + " -> " + b; 
          }
      }
  
      //Triplet with int int int
      public static class Triplet{
  
          public int a;
          public int b;
          public int c;
  
          Triplet(int a , int b, int c){
              this.a = a;
              this.b = b;
              this.c = c;
          }
  
          @Override
          public String toString(){
              return a + " -> " + b; 
          }
      }
  
      //Shortcut function
      public static long lcm(long a , long b){
          return a * (b/gcd(a,b));
      }
  
      //let's make one for calculating lcm basically
      public static int lcm(int a , int b){
          return (a * b)/gcd(a,b);
      }
  
      //int version for gcd
      public static int gcd(int a, int b){
          if(b == 0)
              return a;
              
          return gcd(b , a%b);
      }
  
      //long version for gcd
      public static long gcd(long a, long b){
          if(b == 0)
              return a;
  
          return gcd(b , a%b);
      }
  
      //swapping two elements in an array
      public static void swap(int[] arr, int left , int right){
          int temp = arr[left];
          arr[left] = arr[right];
          arr[right] = temp;
      }
  
      //
       //for char array
       public static void swap(char[] arr, int left , int right){
          char temp = arr[left];
          arr[left] = arr[right];
          arr[right] = temp;
      }
  
      //reversing an array
      public static void reverse(int[] arr){
          int left = 0;
          int right = arr.length-1;
  
          while(left <= right){
              swap(arr, left,right);
              left++;
              right--;
          }
      }
  
       
      //-----------PrintWriter for faster output---------------------------------
      public static PrintWriter out;
        
      //-----------MyScanner class for faster input----------
      public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
   
          public MyScanner() {
              br = new BufferedReader(new InputStreamReader(System.in));
          }
      
          String next() {
              while (st == null || !st.hasMoreElements()) {
                  try {
                      st = new StringTokenizer(br.readLine());
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
              return st.nextToken();
          }
      
          int nextInt() {
              return Integer.parseInt(next());
          }
      
          long nextLong() {
              return Long.parseLong(next());
          }
      
          double nextDouble() {
              return Double.parseDouble(next());
          }
      
          String nextLine(){
              String str = "";
              try {
                  str = br.readLine();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              return str;
          }
  
      }
     //--------------------------------------------------------
  }