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

int32_t main() {

	ios::sync_with_stdio(false);
	cin.tie(nullptr);

#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.in", "w", stdout);
#endif

	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		int a[n];
		map<int, int> m;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			m[a[i]]++;
		}
		set<int> s;
		map<int, int> o;
		for (auto i : m) {
			s.insert(i.second);
			o[i.second]++;
		}
		vector<int> v;
		for (auto i : s) {
			v.push_back(i);
		}
		int sz = v.size();
		int pre[sz], suf[sz];
		pre[0] = v[0] * o[v[0]];
		for (int i = 1; i < sz; i++) {
			pre[i] = pre[i - 1] + v[i] * o[v[i]];
		}

		suf[sz - 1] = o[v[sz - 1]];
		for (int i = sz - 2; i >= 0; i--) {
			suf[i] = suf[i + 1] + o[v[i]];
		}
		int ssuf[sz];
		ssuf[sz - 1] = 0;
		for (int i = sz - 2; i >= 0; i--) {
			ssuf[i] = ssuf[i + 1] + suf[i + 1] * (v[i + 1] - v[i]);
		}
		int best = n;
		for (int i = 0; i < sz; i++) {
			int cur;
			if (i == 0) {
				cur = ssuf[i];
			}
			else {
				cur = pre[i - 1] + ssuf[i];
			}
			best = min(best, cur);
		}
		cout << best << "\n";
	}

	return 0;
}