#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 5'005
#define endl '\n'
const int mod = 998244353;
int n, a[N], dp[N][N];
map<int, int> vis;
bool check(int x)
{
priority_queue<int> q;
int js = 0;
for (int i = 1; i <= n; i++)
{
if (js >= vis[a[i]])
{
q.push(vis[a[i]]);
js -= vis[a[i]];
}
else if (!q.empty() && q.top() > vis[a[i]])
{
js += q.top() - vis[a[i]];
q.pop();
q.push(vis[a[i]]);
js++;
}
else
js++;
}
if (q.size() >= x)
return 1;
return 0;
}
void solve()
{
cin >> n;
vis.clear();
for (int i = 1; i <= n; i++)
{
cin >> a[i];
vis[a[i]]++;
}
sort(a + 1, a + 1 + n);
n = unique(a + 1, a + 1 + n) - a - 1;
int l = 1, r = n, ans = 0;
while (l <= r)
{
int mid = (l + r) >> 1;
if (check(mid))
{
l = mid + 1;
ans = mid;
}
else
r = mid - 1;
}
cout << n - ans << endl;
}
signed main()
{
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}