#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int max_n = 200000;
int n, arr[max_n + 5], cnt2[max_n + 5];
ll psum[max_n + 5];

void solve(void);

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int t;
	cin >> t;
	while (t--) solve();
	return 0;
}

void solve(void) {
	cin >> n;
	int i, j;
	for (i = 0; i < n; i++) { cin >> arr[i]; }

	memset(cnt2, 0, (n + 1) * sizeof(int));

	sort(arr, arr + n);
	i = 0;
	while (i < n) {
		j = i;
		while (j < n && arr[j] == arr[i]) j++;
		cnt2[j - i]++;
		i = j;
	}

	psum[0] = 0LL;
	for (i = 1; i <= n; i++) psum[i] = psum[i - 1] + i * cnt2[i];

	ll ans = (ll)n;
	ll tmp = 0LL, tsum = 0LL;
	for (i = n; i > 0; i--) {
		ans = min(ans, tmp + psum[i - 1]);
		// tmp += cnt2[i];
		tsum += cnt2[i];
		tmp += tsum;
	}

	cout << ans << '\n';
}
