#include <bits/stdc++.h>
#define ll long long
#define endll '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;
const int mod = 1e9 + 7, N = 1e6;
void solve()
{
int n, k;
cin >> n >> k;
int ar[n + 3];
int need[k + 3];
map<int, int> mp;
multiset<int, greater<>> st;
for (int i = 1; i <= n; i++)
cin >> ar[i], mp[ar[i]]++;
for (auto i : mp)
{
st.insert(i.second);
}
// sort(all(v), greater<>());
int ps = 1;
for (int i = 1; i <= n; i++)
{
if (i <= k)
{
need[i] = 1;
}
else
{
need[ps] += 1;
ps++;
if (ps > k)
ps = 1;
}
}
vector<int> v;
for (int i = 1; i <= k; i++)
{
v.pb(need[i]);
}
sort(all(v), greater<>());
int ans = 0;
for (int i = 0; i < k; i++)
{
// cout << v[i] << " = need" << endll;
int x = *st.begin();
st.erase(st.begin());
if(v[i] <= x) {
x -= v[i];
st.insert(x);
}
else {
ans += v[i] - x;
}
}
cout << ans << endl;
// cout << endll;
// cout << endll;
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}