/** これを翻訳している間、あなたはあなたの人生のいくつかの貴重な瞬間を無駄にしました **/
#include<bits/stdc++.h>
using namespace std;

#define rep(i,x,y) for(int i=x;i<y;i++)
#define repr(i,x,y) for(int i=x;i>=y;i--) 
#define int long long
#define pb push_back
#define ff first
#define ss second
#define sz(x) ((int)x.size())
#define all(x) begin(x), end(x)
#define memo(x,y) memset((x),(y),sizeof((x)))
#define line cout<<"[At "<<__LINE__<<"]"<<endl;

using pii = pair<int,int>;
using vi = vector<int>;
using vii = vector<pair<int,int>>;

constexpr int mod = 1e9 + 7 , N = 2e5 + 5;
constexpr long long inf = 1e18;
constexpr long double eps = 1e-6; 


void Onigiri()
{
  int n;
  cin>>n;
  int a[n];
  map<int,int> mpp;
  rep(i,0,n){
    cin>>a[i];
    mpp[a[i]]++;
  }
  vi v;
  for(auto &x:mpp){
    v.pb(x.ss);
  }
  sort(all(v));
  
  if(v[0]==v.back()){cout<<0;return;}
  
  int prev=0,nxt=accumulate(all(v),0LL);
  int ans=n;
  n=sz(v);
  rep(i,0,n){
    int cnt=0,tempsum=0;
    while(i+1<n&&v[i]==v[i+1]){cnt++;tempsum+=v[i];nxt-=v[i];i++;}
    nxt-=v[i];
    //cout<<prev<<"+"<<nxt<<"-"<<v[i]<<"*("<<n-i<<")"<<endl;
    ans=min(ans,prev+nxt-v[i]*(n-i-1));
    prev+=tempsum+v[i];
  }
  cout<<ans;
}
signed main()
{
   ios_base::sync_with_stdio(false);cin.tie(NULL);
   //cout.precision(10);cout<<fixed;
   #ifdef Zoro
   freopen("/home/pritish/Competitive/in", "r", stdin);
   freopen("/home/pritish/Competitive/out", "w", stdout);
   #endif  
 
   int t=1; 
   cin>>t;
 
   while(t--)
   {Onigiri();cout<<"\n";}

   cerr<<"\n"<<(float)clock()/CLOCKS_PER_SEC*1000<<" ms"<<endl;
   return 0;
}