// Don't be dead serious about your life --  it's just like a program
// Never open a ranklist untill and unless you are done with solving problems, wastes 3/4 minutes 
// Do not treat CP as a placement thing, love it and enjoy it, you will succeed for sure.  

#include <bits/stdc++.h>
using namespace std;
typedef  long long  int ll;
typedef long double ld;
typedef unsigned long long int ull;
//fundamentals
#define pb push_back
#define pf push_front
#define lb(v,val)  (lower_bound(v.begin(),v.end(),val)-v.begin())
#define ub(v,val)  (upper_bound(v.begin(),v.end(),val)-v.begin())
#define line cout<<"\n";
#define sortv(v) sort(v.begin(),v.end())
#define sortvd(v) sort(v.begin(),v.end(),greater<ll>())
#define lcm(a,b) (a*b/gcd(a,b))
#define all(v) ((v).begin(),(v).end())
#define v_at_in(n) *p.find_by_order(ll n)
#define mp make_pair
//loops
#define f(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rf(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define f0 for(ll i=0;i<n;i++)
#define f1 for(ll i=1;i<=n;i++)
#define fr for(ll i=n-1;i>=0;i--)
//input of array
#define inarray ll a[n];for(ll i=0;i<n;i++)cin>>a[i];
// starter pack
#define test ll T;cin>>T;while(T--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
//maximum and minimum
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define max4(a,b,c,d) max(a,max3(b,c,d))
#define min4(a,b,c,d) min(a,min3(b,c,d))
#define endl "\n"
//some values
#define PI 3.14159265
const ll MOD=1000000000+7;
const ll MOD1 = 998244353;
const ll Val9=1000000000;
const ll Val6=1000000;
ll pwr(ll x, ll y){
 ll r=1;
 while(y>0){
  if(y&1)
   r=r*x;
  x*=x;
  y>>=1;
 }
 return r;
}

ll pwrm(ll x, ll y, ll m){
 x%=m;
 ll r=1;
 while(y>0){
  if(y&1)
   r=r*x%m;
  x=x*x%m;
  y>>=1;
 }
 return r;
}


int main()
{

// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
fast;
// Shuru karte hai bina kisi backchodi ke_____

    test{
        ll n;cin>>n;inarray
        map<ll,ll>m;
        f(i,0,n)
        m[a[i]]++;
        ll c[n+1];
        f(i,0,n+1)c[i]=0;
        map<ll, ll>::iterator itr; 
        for(itr=m.begin();itr!=m.end();itr++)
        {
            c[itr->second]++;
        }
        ll m1=0,sum=0;
        rf(i,n,0)
        {
            sum=sum+c[i];
            if(c[i]>0)
            {
                m1=max(m1,i*sum);
            }
        }
        cout<<n-m1<<endl;
    }

// End of program Logic

    cerr<<"Time : "<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n";
    return 0;
}

