#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using ll = long long int;
using ull = unsigned long long int;
using vi = vector<int>;
using vl = vector<ll>;
const ll INF = 1000000005;
const ll mod = ((int)1e9 + 7);
const ll mod1 = 998244353;
return (n == 1 || n == 0) ? 1 : (n * ((_fac(n - 1)) % MOD)) % MOD;
}
ll _pow(ll a, ll b, ll MOD = LLONG_MAX) {
  if (b == 0 || a == 1) return 1;
  if (a == 0) return 0;
  if (b & 1) {
    ll temp = _pow(a, b / 2, MOD) % MOD;
    return (((temp * temp) % MOD) * a) % MOD;
  } else {
    ll temp = _pow(a, b / 2, MOD) % MOD;
    return (temp * temp) % MOD;
  }
}
long long _powm(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
ll _gcd(ll a, ll b) {
  if (b == 0) return a;
  return _gcd(b, a % b);
}
ll _lcm(ll a, ll b) { return (a * b) / _gcd(a, b); }
ll _nPr(ll n, ll r) { return _fac(n) / _fac(r); }
ll _nCr(ll n, ll r) {
  if (r > n - r) r = n - r;
  if (r == 0) return 1;
  ll n1 = 1;
  ll n2 = 1;
  for (auto i = (1); i <= (r); i++) n1 *= i;
  for (auto i = (n - r + 1); i <= (n); i++) n2 *= i;
  return n2 / n1;
}
ll _invMod(ll a, ll m) { return _pow(a, m - 2, m); }
ll _cat(int n) { return _nCr(2 * n, n) / (n + 1); }
ll _msb(ll n) {
  n |= n >> 1;
  n |= n >> 2;
  n |= n >> 4;
  n |= n >> 8;
  n |= n >> 16;
  n |= n >> 32;
  n++;
  return (n >> 1);
}
int _nod(ll n) {
  int c = 0;
  while (n > 0) {
    c++;
    n /= 10;
  }
  return c;
}
int _nob(ll n) {
  int c = 0;
  while (n > 0) {
    c++;
    n &= (n - 1);
  }
  return c;
}
bool _isPrime(ll n, int k = 5) {
  if (n <= 1 || n == 4) return false;
  if (n <= 3) return true;
  while (k > 0) {
    ll a = 2 + rand() % (n - 4);
    if (_gcd(n, a) != 1) return false;
    if (_pow(a, n - 1, n) != 1) return false;
    k--;
  }
  return true;
}
ll _lsb(ll n) { return (n & (-n)); }

public:
_rand_() { func(); }
void func() { srand((unsigned)time(0)); }
}
;
_rand_ _rno_;
clock_t _tstart, _tend;
class ___ {
 public:
  ___() { b(); }
  ~___() { e(); }
  void b() { _tstart = clock(); }
  void e() { _tend = clock(); }
};
___ __;
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int _tc;
cin >> _tc;
while (_tc--) {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > v(n * m);
  for (auto i = (0); i <= (n * m - 1); i++) {
    int a;
    cin >> a;
    v[i] = {a, i};
  }
  sort(v.begin(), v.end());
  vector<vector<pair<int, int> > > matrix(n, vector<pair<int, int> >(m));
  int _ = 0;
  for (auto i = (0); i <= (n - 1); i++) {
    for (auto j = (0); j <= (m - 1); j++) {
      matrix[i][j] = v[_++];
    }
  }
  int ans = 0;
  for (auto i = (0); i <= (n - 1); i++) {
    for (auto j = (1); j <= (m - 1); j++) {
      for (auto k = (0); k <= (j - 1); k++) {
        if (matrix[i][k].first < matrix[i][j].first &&
            matrix[i][k].second < matrix[i][j].second)
          ans++;
      }
    }
  }
  cout << ans;
  cout << "\n";
  ;
}
return 0;
}
