#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void solve(int cs) {
int n, k;
cin >> n >> k;
map<int,int> cnt;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cnt[x] += 1;
}
multiset<int> S;
for (auto &it : cnt) S.insert(it.second);
vector<int> cc(k, n / k);
for (int i = 0; i < (n % k); i++) {
cc[i] += 1;
}
multiset<int> need;
for (auto &it : cc) need.insert(it);
int ans = 0;
while (need.size()) {
int have = *S.rbegin();
S.erase(--S.end());
int del = *need.rbegin();
need.erase(--need.end());
if (have > del) {
S.insert(have - del);
} else if (have < del) {
del -= have;
ans += del;
while (del > 0) {
int small = *S.begin();
S.erase(S.begin());
if (small > del) {
S.insert(small - del);
break;
} else if (small == del) {
break;
} else {
del -= small;
}
}
}
}
cout << ans << "\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;
}