#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <iostream>
//#include <bits/stdc++.h>
using namespace std;
#include <string.h>
#include <utility>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <math.h>
#define ll long long
ll max(ll a, ll b)
{
    if (a < b)
        return b;
    return a;
}
ll min(ll a, ll b)
{
    if (a < b)
        return a;
    return b;
}
#define INPUT_OUTPUT freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define all(v) v.begin(), v.end()
#define lli unsigned long long int
#define f(i, a, b) for (int i = a; i < b; i++)
#define here cout << "here\n"
#define pp pair<ll, ll>
#define vpp vector<pp>
#define vii vector<ll>
#define ss second
#define ff first
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define endl "\n"
#define inside ll, vector<ll>, greater<ll>
#define Minpq priority_queue<inside>
#define Maxpq priority_queue<ll>
#define unm unordered_map<ll, ll>
#define MAX (1LL << 60) - 1
#define MIN -((1LL << 60) - 1)

ll X[] = {-1, 1, 0, 0};                // 4
ll Y[] = {0, 0, -1, 1};                // 4
ll XX[] = {-1, 1, 0, 0, -1, 1, -1, 1}; // 8
ll YY[] = {0, 0, -1, 1, -1, 1, 1, -1}; // 8
ll HX[] = {1, -1, 2, -2, 1, -1, -2, 2};
ll HY[] = {2, -2, 1, -1, -2, 2, 1, -1};
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0)

// class req
// {
// public:
//     ll len, l, r;
//
//     req(ll a, ll b, ll c)
//         : len(a), l(b), r(c) {}
// };
// make sure that b's value is the desired top values, write priority_queue<req> if spent is max and if spent is equal than ascending by ind
// bool operator<(const req &a, const req &b)
// {
//     if (a.len != b.len)
//         return a.len < b.len; // returns true if descending sort of spent is req
//
//     return a.l > b.l; // return true if ascending sort of ind is req
// }
ll fpow(ll x, ll n)
{
    ll res = 1;
    while (n)
    {
        if (n & 1)
        {
            res = res * x % mod;
        }
        x = x * x % mod;
        n >>= 1;
    }
    return (res % mod);
}

ll binomialCoeff(ll n, ll k)
{
    ll C[n + 1][k + 1];
    ll i, j;
    C[0][0] = 1;
    for (i = 1; i <= n; i++)
    {
        for (j = 0; j <= min(i, k); j++)
        {
            // Base Cases
            if (j == 0 || j == i)
                C[i][j] = 1;

            else
                C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
        }
    }

    return C[n][k];
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

ll lcm(ll a, ll b)
{
    return a * (b / gcd(a, b));
}

// Max and min distribute over each other like gcd and lcm, max of min of all pair having A as common is min of A and max of rest, max(min(A,B), min(A,C)) = min(A, max(B,C))
// Neutral element for AND = -1 ( all ones ) , SUM = 0, MULTI = 1, OR = 0 (all zeros )
// advice - always check constraints
// 1. brute force
// 2. trick , think about: this - (combinations), i & i+k, n^2 to n
// 3. gcd/hcf
// 4. reverse array, find max, find min, sorting, heap, tree
// 5. graph, bfs, dfs
//https://cp-algorithms.com
//https://oeis.org
//vector<vector<ll>> v(n, vector<ll>(m, 0))
// ll **arr = new ll*[n]; f(i,0,n){ arr[i] = new ll [m]};
/*
1. segmentTree, SQRT decomposition, BIT
2. User Comparator, PriorityQueue, Stack, Queue, Deque
3. Sorting, Recursion
4. If a+b=n then if either of them is greater than n/2 other is less than n/2 for sure
5. For less than 1e12, use normal to find prime factor, else 902076349729 = 949777*949777,  916517107801 = 957349*957349, 506623120463 = 47^(x), 34359738368 = 2^(x), 912943012301/974059904437/930881829259 = prime
6. There is prime between n and 3n/2 for n<=1000. WOH !
7. Prime gap of numbers less than 1e9 doesn't exceed 300
8. Combinotrics- if you have n identical object and r different object number of ways of arrangement is (n+r-1) C (r-1) or (n+r-1) C (n)
*/

ll getAnswer(vii &v, ll x)
{
    ll ans = 0;
    for (int i = 0; i < v.size(); i++)
    {
        if (v[i] < x)
        {
            ans += v[i];
        }
        else
            ans += v[i] - x;
    }

    return ans;
}

void solve()
{
    ll n;
    cin >> n;
    ll arr[n];
    f(i, 0, n) cin >> arr[i];
    sort(arr, arr + n);
    vii v;
    for (int i = 0; i < n;)
    {
        int j = i;
        while (j < n && arr[i] == arr[j])
            j++;

        v.pb(j - i);
        i = j;
    }

    n = v.size();

    sort(all(v));

    // f(i, 0, v.size()) cout << v[i] << " ";
    // cout << "\n";

    ll sum = 0;
    f(i, 0, n) sum += v[i];

    ll cursum = 0;
    ll fans = MAX;
    for (int i = 0; i < n; i++)
    {
        ll remsum = sum - cursum;
        ll ans = cursum + remsum - (n - i) * v[i];
        cursum += v[i];

        fans = min(ans, fans);
    }

    cout << fans << "\n";
}
int32_t main()
{
    IOS;
    ll test = 0;
    cin >> test;
    while (test--)
        solve();
}