#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;

#define rep(i, a, n) for (int i = a; i < n; i++)
#define show_vi(a)        \
    cout << #a << "-->";  \
    for (auto x : a)      \
        cout << x << " "; \
    cout << endl;

int mod = (int)1e9 + 7;

void solve()
{
    int n;
    cin >> n;
    vi a(n), count(n+1);

    rep(i, 0, n) cin >> a[i];
    sort(a.begin(), a.end());

    int len=1;
    rep(i, 1, n)
    {
        if(a[i]==a[i-1])
            len++;
        else
        {
            count[len]++;
            len=1;
        }
    }
    count[len]++;

    //show_vi(count);

    int m=0, x=0;
    for(int i=n; i>0; i--)
    {
        m=max(m, i*(count[i]+x));
        x+=count[i];
    }
    
    cout<<n-m<<"\n";
}

int main()
{
    cin.tie(0), cout.tie(0)->sync_with_stdio(0);
    int tc;
    cin >> tc;
    while (tc--)
    {
        solve();
    }

    return 0;
}