﻿#define _CRT_SECURE_NO_WARNINGS

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

#define FOR(i,n) for (int i = 0; i < n; ++i)
#define FORN(i, a, b) for (int i = a; i < b; ++i)
#define FORE(i, a, b) for (int i = a; i <= b; ++i)
#define DOWN(i, n) for (int i = n - 1; i >= 0; --i)
#define FORX(i, c) for(decltype(c.begin())i=c.begin();i!=c.end();++i)
#define pb push_back
#define sqr(x) ((x) * (x))
#define si(x) int(x.size())
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define fi first
#define se second

using ll = long long; using LL = long long;
using ull = unsigned long long; using ULL = unsigned long long;
using ldb = long double; using LDB = long long;
using str = string;
using pi = pair<int, int>; using PII = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>; using VI = vector<int>;
using vvi = vector<vi>; using VVI = vector<VI>;
using vl = vector<ll>; using VL = vector<LL>;
using vb = vector<bool>;
using vd = vector<double>;
using vvl = vector<vl>; using VVL = vector<VL>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;

void solve();

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(0);

#ifdef _DEBUG_89
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif // _DEBUG_89

	int T = 1;
	cin >> T;
	while (T--) {
		solve();
	}
	cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
}

template<typename T> void print_vec(vector<T>& v) {
	for (auto& e : v) cout << e << " ";
	cout << "\n";
}

inline string yesno(bool val) { return val ? "YES" : "NO"; }

const int MOD = 1e6;
const ll INF = 1e17;

void solve() {
	int n;
	cin >> n;
	vl a(n + 1);
	FORE(i, 1, n) {
		cin >> a[i];
	}
	sort(all(a));
	vl c = {0};
	FORE(i, 1, n) {
		if (a[i] != a[i - 1]) c.push_back(0);
		c.back()++;
	}

	sort(all(c));
	n = c.size() - 1;
	vl sums(n + 1);
	FORE(i, 1, n) {
		sums[i] = sums[i - 1] + c[i];
	}

	ll ans = INF;
	FORE(i, 1, n) {
		ll val = sums[i - 1];
		val += sums[n] - sums[i] - (n - i) * c[i];
		ans = min(ans, val);
	}
	cout << ans << "\n";
}