#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi pair<int,int>
#define pl pair<ll,ll>
#define pd pair<double,double>
#define F first
#define S second
#define pb push_back
#define all(v) v.begin(),v.end()
#define mod 1000000007

int main(){
    //cout<<ceil((double)log2(3));
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        map<int,int>m;
        for(int i=0;i<n;i++){
            cin>>a[i];
            m[a[i]]++;
        }
        map<int,int>im;
        for(auto x:m){
            im[x.S]++;
        }
        vector<pi>v;
        for(auto x:im){
            v.pb({x.F,x.S});
        }
        int ans=INT_MAX;
        int p=v.size();
        assert(p<=1000);
        for(int i=0;i<p;i++){
            int s=0;
            for(int j=0;j<p;j++){
                if(v[j]>=v[i]){
                    s+=abs(v[j].F-v[i].F)*v[j].S;
                }
                else{
                    s+=v[j].F*v[j].S;
                }
            }
            ans=min(ans,s);
        }
        cout<<ans<<endl;

    }

}
