#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <queue>
#include <bitset>
#include <unordered_map>
#include <complex>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define double long double
#define sqr(x) (x)*(x)
#define pb push_back
typedef int C;
typedef complex<C>P;
#define X real()
#define Y imag()
int32_t main()
{
	IOS;
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		vector<int>a(n);
		unordered_map<int, int>m;
		m.max_load_factor(0.25);
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			m[a[i]]++;
		}
		map<int, int>res;
		for (auto&c : m) {
			res[c.second]++;
		}
		int ans = 1e18;
		int add = 0;
		int i = 1;
		int cc = m.size();
		for (auto&c : res) {
			ans = min(ans, n-cc*c.first);
			//add += c.second*c.first;
			cc -= c.second;
		}
		cout << ans << "\n";
	}
}