https://codeforces.com/contest/1183/problem/D
==sort(num.rbegin(),num.rend());==逆序
#include<bits/stdc++.h>
#include<deque>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define forn(i,n) for(int i=0;i<n;i++)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef long long ll;
const int maxn=3e5+5;
const int INF=0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
int a[maxn],vis[maxn];
int q,n,cnt,x;
int main(){
cin>>q;
while(q--){
cin>>n;
vector<int> num(n+1);
rep(i,1,n){
cin>>x;
num[x]++;
}
sort(num.rbegin(),num.rend());
int lst=num[0];
int ans=num[0];
// cout<<num[1]<<endl;
for(int i=1;i<=n;i++){
if(lst==0)
break;
if(num[i]>=lst){
ans+=(lst-1);
lst--;
}
else
{
ans+=num[i];
lst=num[i];
}
}
cout<<ans<<endl;
}
return 0;
}