/*
* Copyright (c) 2024 Emon Thakur
* All rights reserved.
*/
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int x,k,n; cin>>n>>k;
map<int,int>m;
for(int i=0;i<n;i++)
{
cin>>x;
m[x]++;
}
priority_queue<int>pq;
for(auto e:m) pq.push(e.second);
int g = n/k;
int r = n%k;
int need = g+1;
int ans = 0;
for(int i=0;i<r;i++)
{
int tp = pq.top();
pq.pop();
ans += max(0,need-tp);
tp = max(0,tp-need);
if(tp>0) pq.push(tp);
}
need = g;
for(int i=r;i<k;i++)
{
int tp = pq.top();
pq.pop();
ans += max(0,need-tp);
tp = max(0,tp-need);
if(tp>0) pq.push(tp);
}
cout<<ans<<endl;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
int t; cin>>t; while(t--) solve();
}