#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int T;
cin >> T;
while(T--){
long long N, K;
cin >> N >> K;
vector<long long> A(N);
for(auto &x : A) cin >> x;
// Sort the array to count frequencies efficiently
sort(A.begin(), A.end());
// Count frequencies
vector<long long> freqs;
if(N > 0){
long long current = A[0];
long long cnt = 1;
for(int i = 1; i < N; i++){
if(A[i] == current){
cnt++;
}
else{
freqs.push_back(cnt);
current = A[i];
cnt = 1;
}
}
freqs.push_back(cnt); // Push the count of the last element
}
// Sort frequencies in descending order
sort(freqs.begin(), freqs.end(), greater<long long>());
// Determine group sizes
long long Q = N / K; // Base size of each group
long long R = N % K; // Number of groups that will have an extra element
vector<long long> group_sizes;
for(int i = 0; i < R; i++) group_sizes.push_back(Q + 1);
for(int i = 0; i < K - R; i++) group_sizes.push_back(Q);
// Sort group sizes in descending order
sort(group_sizes.begin(), group_sizes.end(), greater<long long>());
// Initialize max heap with frequencies
priority_queue<long long> heap;
for(auto c : freqs){
heap.push(c);
}
long long coverage = 0;
for(auto s : group_sizes){
if(heap.empty()) break;
long long c = heap.top();
heap.pop();
if(c >= s){
coverage += s;
if(c - s > 0){
heap.push(c - s);
}
}
else{
coverage += c;
// No need to push back if c - s <= 0
}
}
// The minimum number of changes is total elements minus the coverage
long long minimal_changes = N - coverage;
cout << minimal_changes << "\n";
}
}