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

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);}
template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);}
template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); }
template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n);  for(auto& a : v) init(a, x, m...); }


int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);
    int T = 1;
    cin >> T;
    FOR1(t, T){
        int n;
        cin >> n;
        vi a(n);
        map<int, int> H;
        FOR(i, n) {
            cin >> a[i];
            H[a[i]]++;
        }
        map<int , int> H2; //cnt of cnts
        int groups = H.size();
        for(auto it : H) {
            H2[it.second]++;
        }

//        int removed = 0;
        int sol = n;
        for(auto it : H2){
            umin(sol, n - groups * it.first);
            groups -= it.second;
//            removed += it.first * it.second;
        }
        cout << sol << endl;
    }


    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}

