#include <bits/stdc++.h>
#define ll long long
using namespace std;
int t, n, ma[200010];
ll a[200010];
map <ll, ll> m, flag;
int main(){
	scanf("%d", &t);
	while(t--){
		int maxn = 0;
		scanf("%d", &n);
		for(int i = 1; i <= n; i++){
			scanf("%lld", &a[i]);
			m[a[i]]++;
			if(m[a[i]] > maxn) maxn = m[a[i]];
		}
		
		for(int i = 1; i <= n; i++){
			if(!flag[a[i]]){
				flag[a[i]] = 1;
				ma[m[a[i]]]++;
			}
		}
		ll minn = 1e18, cnt = 0, cnt1 = 0;
		for(int i = maxn; i >= 0; i--){
			minn = min(minn, n - cnt - (ma[i] * i) + (cnt - cnt1 * i));
			cnt += ma[i] * i;
			cnt1 += ma[i];
		}
		for(int i = 1; i <= n; i++){
			ma[m[a[i]]] = 0;
			flag[a[i]] = 0;
			m[a[i]] = 0;
		}
		printf("%lld\n", minn);
	}
	return 0;
}