#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#ifndef ONLINE_JUDGE
#include "Ash.cpp"
#else
#define dbg(...)
#define dbgA(...)
#endif
void solve(int cs) {
int n, k;
cin >> n >> k;
assert(n % k == 0);
map<int,int> cnt;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cnt[x] += 1;
}
int res = 0;
int g = n / k;
for (auto it : cnt) {
res += it.second % g;
}
cout << res / 2 << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
cin >> tc;
for (int cs = 1; cs <= tc; cs++) {
solve(cs);
}
return 0;
}