#include <bits/stdc++.h>   #pragma GCC target( avx2 )   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   using namespace std;   template <class T, class U>   ostream &operator<<(ostream &os, const pair<T, U> &p) {     os <<  (  << p.first <<  ,  << p.second <<  ) ;     return os;   }   template <class T>   ostream &operator<<(ostream &os, const vector<T> &v) {     os <<  { ;     for (int i = 0; i < (int)v.size(); i++) {       if (i) {         os <<  , ;       }       os << v[i];     }     os <<  } ;     return os;   }   ostream &operator<<(ostream &os, const set<int> &se) {     os <<  { ;     int now = 0;     for (auto x : se) {       if (now) {         os <<  , ;       }       os << x;       now++;     }     os <<  } ;     return os;   }   void debugg() { cerr << endl; }   template <class T, class... Args>   void debugg(const T &x, const Args &...args) {     cerr <<     << x;     debugg(args...);   }   using namespace std;   const int inf = (1 << 30) - 1;   const long long linf = 1LL << 61;   const int MAX = 510000;   int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};   int dx[8] = {-1, 0, 1, 0, 1, -1, 1, -1};   const double pi = acos(-1);   const double eps = 1e-8;   template <typename T1, typename T2>   inline bool chmin(T1 &a, T2 b) {     if (a > b) {       a = b;       return true;     } else       return false;   }   template <typename T1, typename T2>   inline bool chmax(T1 &a, T2 b) {     if (a < b) {       a = b;       return true;     } else       return false;   }   template <typename T>   inline void print(T &a) {     int sz = a.size();     for (auto itr = a.begin(); itr != a.end(); itr++) {       cout << *itr;       sz--;       if (sz) cout <<    ;     }     cout <<   n ;   }   template <typename T1, typename T2>   inline void print2(T1 a, T2 b) {     cout << a <<     << b <<   n ;   }   template <typename T1, typename T2, typename T3>   inline void print3(T1 a, T2 b, T3 c) {     cout << a <<     << b <<     << c <<   n ;   }   void mark() {     cout <<  #           <<   n ;   }   long long pcount(long long x) { return __builtin_popcountll(x); }   const int mod = 998244353;   template <typename T>   struct BIT {     vector<T> dat;     long long sz;     BIT(long long sz) : sz(sz) { dat.assign(++sz, 0); }     T sum(long long k) {       T ret = 0;       for (++k; k > 0; k -= k & -k) ret += dat[k];       return (ret);     }     void add(long long k, T x) {       for (++k; k < dat.size(); k += k & -k) dat[k] += x;     }     long long get(T k) {       if (k <= 0) return 0;       long long ret = 0;       int n = 1;       while (n < sz) n *= 2;       for (int i = n / 2; i > 0; i /= 2) {         if (ret + i < sz && dat[ret + i] < k) {           k -= dat[ret + i];           ret += i;         }       }       return ret;     }   };   void solve() {     int n, m;     cin >> n >> m;     vector<pair<long long, long long>> a(n * m);     vector<long long> b(n * m);     for (int i = 0; i < (n * m); i++) {       cin >> a[i].first;       a[i].second = i;       b[i] = a[i].first;     }     sort((a).begin(), (a).end());     vector<vector<long long>> c(n, vector<long long>(m));     for (int i = 0; i < (n); i++)       for (int j = 0; j < (m); j++) {         c[i][j] = a[i * m + j].second;       }     for (int i = 0; i < (n); i++)       for (int j = 0; j < (m); j++) {         int k = j + 1;         while (k < m && b[c[i][k]] == b[c[i][j]]) k++;         reverse(c[i].begin() + j, c[i].begin() + k);         j = k - 1;       }     vector<pair<long long, long long>> pos(n * m);     for (int i = 0; i < (n); i++)       for (int j = 0; j < (m); j++) {         pos[c[i][j]] = {i, j};       }     long long ans = 0;     vector<BIT<long long>> bit(n, BIT<long long>(m));     for (int i = 0; i < (n * m); i++) {       ans += bit[pos[i].first].sum(pos[i].second);       bit[pos[i].first].add(pos[i].second, 1);     }     cout << ans <<   n ;   }   int main() {     cin.tie(nullptr);     ios::sync_with_stdio(false);     int t = 1;     cin >> t;     while (t--) {       solve();     }   }