#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<ll>v;
vector<pair<ll,ll> >vv;
map<ll,ll>m,mm;
ll n,x,sum,q,y;
int main()
{
    cin>>q;

    while(q--)
    {
        sum=0;
        cin>>n;
        for(int i=0; i<n; i++)
        {
            cin>>x>>y;
            m[x]++;
            if(y)
                mm[x]++;

        }
        map<ll,ll> ::iterator it=m.begin();
        for(; it!=m.end(); it++)
        {
            x=(*it).second;
            v.push_back(x);
            if(mm[(*it).first])
               vv.push_back( {x,mm[(*it).first] } );
        }
        sort(v.begin(),v.end(),greater<ll>());
        ll p=v[0]+1;
        for(ll i=0;i<v.size();i++)
        {
          //  cout<<x<<" ";
            x=v[i];
            if(x<p-1)
                p=x+1;
            while(x>=p)
                x--;
            sum+=x;
            p--;
          //  cout<<x<<" ";
            if(p==0)
                break;
        }
        cout<<sum<<" ";
        m.clear();
       v.clear();
       sum=0;

        sort(vv.begin(),vv.end(),greater<pair<ll,ll> >());
         p=vv[0].first+1;
        for(ll i=0;i<vv.size();i++)
        {

            x=vv[i].first;
            // cout<<x<<"  "<<vv[i].second<<" t t  ";
            if(x<p-1)
                p=x+1;
            while(x>=p)
                x--;
                if(x>vv[i].second)
            sum+=vv[i].second;
            else
                sum+=x;
            p--;
          //  cout<<x<<" ";
            if(p==0)
                break;
        }
        cout<<sum<<endl;
        mm.clear();
       vv.clear();

    }

    return 0;
}
//dvfbrtnyterewtgthtggh


/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 200005
#define maxm 1000006
#define pii pair <int,int>
#define Task ""
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n,t[maxn << 2],top;
pii cnt[maxn];
vector <int> save;
void Enter()
{
    cin >> n;
    FOR(i,1,n) cnt[i].F = cnt[i].S = 0;
    FOR(i,1,n)
    {
        int x,y;
        cin >> x >> y;
        ++cnt[x].F;
        if (y) ++cnt[x].S;
    }
    sort(cnt + 1,cnt + n + 1);
}
void Update(int l,int r,int id,int x)
{
    if (l == r)
    {
        t[id] = 1;
        return;
    }
    int mid = (l+r) >> 1;
    if (mid >= x) Update(l,mid,2*id,x);
    else Update(mid+1,r,2*id+1,x);
    t[id] = min(t[2*id],t[2*id+1]);
}
int Get(int l,int r,int id,int u,int v)
{
    if (l > v || r < u) return 1;
    if (u <= l && r <= v) return t[id];
    int mid = (l+r) >> 1;
    return min(Get(l,mid,2*id,u,v),Get(mid+1,r,2*id+1,u,v));
}
void Pro1()
{
    int ans = 0;
    save.clear();
    FOR(i,1,n << 2) t[i] = 0;
    FORD(i,n,1)
    {
        int l = 0,r = cnt[i].F + 1;
        while (r - l > 1)
        {
            int mid = (l+r) >> 1;
            int ww = Get(1,n,1,mid,cnt[i].F);
            if (ww) r = mid;
            else l = mid;
        }
        ans += l;
        if (l)
        {
            Update(1,n,1,l);
            save.pb(l);
        }
    }
    cout << ans << ' ';
}
int Max(int x,int y)
{
    if (cnt[x].S != cnt[y].S) return cnt[x].S < cnt[y].S ? y : x;
    return x < y ? y : x;
}
void Build(int l,int r,int id)
{
    if (l == r)
    {
        t[id] = l;
        return;
    }
    int mid = (l+r) >> 1;
    Build(l,mid,2*id);
    Build(mid+1,r,2*id+1);
    t[id] = Max(t[2*id],t[2*id+1]);
}
int Get2(int l,int r,int id,int u,int v)
{
    if (l > v || r < u) return 0;
    if (u <= l && r <= v) return t[id];
    int mid = (l+r) >> 1;
    return Max(Get2(l,mid,2*id,u,v),Get2(mid+1,r,2*id+1,u,v));
}
void Update2(int l,int r,int id,int x)
{
    if (l == r)
    {
        cnt[l].S = 0;
        return;
    }
    int mid = (l+r) >> 1;
    if (mid >= x) Update2(l,mid,2*id,x);
    else Update2(mid+1,r,2*id+1,x);
    t[id] = Max(t[2*id],t[2*id+1]);
}
void Pro2()
{
    //for (int v : save) cout << v << ' ';
    int ans = 0;
    Build(1,n,1);
    int l = n;
    for (int v : save)
    {
        while (l && cnt[l].F >= v) --l;
        int ww = Get2(1,n,1,l+1,n);
        ans += min(v,cnt[ww].S);
        Update2(1,n,1,ww);
    }
    cout << ans << '\n';
}
int main()
{
    ios_base::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL);
    //freopen(Task".inp", "r",stdin);
    //freopens(Task".out", "w",stdout);
    int test;cin >> test;
    while (test--)
    {
        Enter();
        Pro1();
        Pro2();
    }
    return 0;
}


/**GreenGrass**/
#include <bits/stdc++.h>
#define Ford(i,a,b) for(int i=a;i>=b;--i)
#define Forr(i,a,b) for(int i=a;i<b;++i)
#define For(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
#define mp make_pair
#define S second
#define F first
using namespace std;
template <typename T> inline void Min(T &a, T b){if(a>b) a=b;}
template <typename T> inline void Max(T &a, T b){if(a<b) a=b;}
typedef vector <int> vi;
typedef pair <int,int> ii;
typedef vector <ii> vii;
typedef pair <ii,int> iii;
const int N = 100005;
ii a[N];
priority_queue <int> Q;
int sum, run, n, le[N], q[N], cnt, q2[N], top;
int Green(){
	run = 2e9;
	sum = 0;
	Ford(i,n,1){
		Min(run, a[i].F);
		q[++cnt] = run;
		sum += run;
		--run;
		if(run<1) break;
	}
	For(i,1,cnt/2) swap(q[i], q[cnt-i+1]);
	return sum;
}
void Handle(int x){
	if(!x) return;
	int lef = lower_bound(a+1,a+1+n,mp(q[le[x]], 0))-a;
	int rig = upper_bound(a+1,a+1+n,mp(q[x], N))-a-1;
	while(Q.size()) Q.pop();
	For(i,lef,rig) Q.push(a[i].S);
	int am = x-le[x]+1;
	Ford(i,x,le[x]){
		sum += min(q[i],Q.top());
		Q.pop();
	}
	Handle(le[x]-1);
}
int Grass(){
	For(i,1,cnt){
		int x = max(1, i-(upper_bound(a+1,a+1+n,mp(q[i], N))-lower_bound(a+1,a+1+n,mp(q[i], 0)))+1);
		Min(x, i);
		le[i] = x;
		while(top && q2[top]>=x){
			Min(le[i], le[q2[top--]]);
		}
		q2[++top] = i;
	}
	Handle(cnt);
	return sum;
}
void In(){
	sum = top = cnt = 0;
	cin >> n;
	For(i,1,n){
		int x, y;
		cin >> x >> y;
		++a[x].F;
		if(y) ++a[x].S;
	}
	sort(a+1,a+1+n);
	cout << Green() << ' ';
	sum = 0;
	cout << Grass() << '\n';
	For(i,1,n) a[i] = {0,0};
}
void Out(){
	int T;
	cin >> T;
	while(T--){
		In();
	}
}
int main(){
    ios_base::sync_with_stdio(NULL);
    cin .tie(NULL); cout .tie(NULL);
   // freopen(".inp ", "r", stdin);
//    freopen(".out", "w", stdout);
	Out();
}


/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 200005
#define maxm 1000006
#define pii pair <int,int>
#define Task ""
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n,t[maxn << 2],top,cnt[maxn],cnt1[maxn];
void Enter()
{
    cin >> n;
    FOR(i,1,n) cnt[i] = cnt1[i] = 0;
    FOR(i,1,n)
    {
        int x,y;
        cin >> x >> y;
        ++cnt[x];
        if (y) ++cnt1[x];
    }
    sort(cnt + 1,cnt + n + 1);
    sort(cnt1 + 1,cnt1 + n + 1);
}
void Update(int l,int r,int id,int x)
{
    if (l == r)
    {
        t[id] = 1;
        return;
    }
    int mid = (l+r) >> 1;
    if (mid >= x) Update(l,mid,2*id,x);
    else Update(mid+1,r,2*id+1,x);
    t[id] = min(t[2*id],t[2*id+1]);
}
int Get(int l,int r,int id,int u,int v)
{
    if (l > v || r < u) return 1;
    if (u <= l && r <= v) return t[id];
    int mid = (l+r) >> 1;
    return min(Get(l,mid,2*id,u,v),Get(mid+1,r,2*id+1,u,v));
}
void Pro(int a[])
{
    int ans = 0;
    FOR(i,1,n << 2) t[i] = 0;
    FORD(i,n,1)
    {
        int l = 0,r = a[i] + 1;
        while (r - l > 1)
        {
            int mid = (l+r) >> 1;
            int ww = Get(1,n,1,mid,a[i]);
            if (ww) r = mid;
            else l = mid;
        }
        ans += l;
        if (l) Update(1,n,1,l);
    }
    cout << ans;
}
int main()
{
    ios_base::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL);
    //freopen(Task".inp", "r",stdin);
    //freopens(Task".out", "w",stdout);
    int test;cin >> test;
    while (test--)
    {
        Enter();
        Pro(cnt);
        cout << ' ';
        Pro(cnt1);
        cout << '\n';
    }
    return 0;
}


/**GreenGrass**/
#include <bits/stdc++.h>
#define Ford(i,a,b) for(int i=a;i>=b;--i)
#define Forr(i,a,b) for(int i=a;i<b;++i)
#define For(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
#define mp make_pair
#define S second
#define F first
using namespace std;
template <typename T> inline void Min(T &a, T b){if(a>b) a=b;}
template <typename T> inline void Max(T &a, T b){if(a<b) a=b;}
typedef vector <int> vi;
typedef pair <int,int> ii;
typedef vector <ii> vii;
typedef pair <ii,int> iii;
const int N = 200005;
ii a[N];
priority_queue <int> Q;
int sum, run, n, le[N], q[N], cnt, q2[N], top;
int Green(){
	run = N;
	Ford(i,n,1){
		Min(run, a[i].F);
		q[++cnt] = run;
		sum += run;
		--run;
		if(run<1) break;
	}
	For(i,1,cnt/2) swap(q[i], q[cnt-i+1]);
	return sum;
}
void Handle(int x){
	if(!x) return;
	int lef = lower_bound(a+1,a+1+n,mp(q[le[x]], 0))-a;
	int rig = upper_bound(a+1,a+1+n,mp(q[x], N))-a-1;
	while(Q.size()) Q.pop();
	For(i,lef,rig) Q.push(a[i].S);
	Ford(i,x,le[x]){
		if(Q.empty()){
			cout << -1;
			exit(0);
		}
		sum += min(q[i],Q.top());
		Q.pop();
	}
	Handle(le[x]-1);
}
int Grass(){
	For(i,1,cnt){
		int x = max(1, i-(upper_bound(a+1,a+1+n,mp(q[i], N))-lower_bound(a+1,a+1+n,mp(q[i], 0)))+1);
		Min(x, i);
		le[i] = x;
		while(top && q2[top]>=x){
			Min(le[i], le[q2[top]]);
			--top;
		}
		q2[++top] = i;
	}
	Handle(cnt);
	return sum;
}
void In(){
	sum = top = cnt = 0;
	cin >> n;
	For(i,1,n){
		int x, y;
		cin >> x >> y;
		++a[x].F;
		if(y) ++a[x].S;
	}
	sort(a+1,a+1+n);
	cout << Green() << ' ';
	sum = 0;
	cout << Grass() << '\n';
	For(i,1,n) a[i] = {0,0};
}
void Out(){
	int T;
	cin >> T;
	while(T--){
		In();
	}
}
int main(){
    ios_base::sync_with_stdio(NULL);
    cin .tie(NULL); cout .tie(NULL);
   // freopen(".inp ", "r", stdin);
//    freopen(".out", "w", stdout);
	Out();
}


/**GreenGrass**/
#include <bits/stdc++.h>
#define Ford(i,a,b) for(int i=a;i>=b;--i)
#define Forr(i,a,b) for(int i=a;i<b;++i)
#define For(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
#define mp make_pair
#define S second
#define F first
using namespace std;
template <typename T> inline void Min(T &a, T b){if(a>b) a=b;}
template <typename T> inline void Max(T &a, T b){if(a<b) a=b;}
typedef vector <int> vi;
typedef pair <int,int> ii;
typedef vector <ii> vii;
typedef pair <ii,int> iii;
const int N = 100005;
ii a[N];
priority_queue <int> Q;
int sum, run, n, le[N], q[N], cnt, q2[N], top;
int Green(){
	run = 2e9;
	sum = 0;
	Ford(i,n,1){
		Min(run, a[i].F);
		q[++cnt] = run;
		sum += run;
		--run;
		if(run<1) break;
	}
	For(i,1,cnt/2) swap(q[i], q[cnt-i+1]);
	return sum;
}
void Handle(int x){
	if(!x) return;
	int lef = lower_bound(a+1,a+1+n,mp(q[le[x]], 0))-a;
	int rig = upper_bound(a+1,a+1+n,mp(q[x], N))-a-1;
	while(Q.size()) Q.pop();
	For(i,lef,rig) Q.push(a[i].S);
	Ford(i,x,le[x]){
		if(Q.empty()){
			cout << -1;
			exit(0);
		}
		sum += min(q[i],Q.top());
		Q.pop();
	}
	Handle(le[x]-1);
}
int Grass(){
	For(i,1,cnt){
		int x = max(1, i-(upper_bound(a+1,a+1+n,mp(q[i], N))-lower_bound(a+1,a+1+n,mp(q[i], 0)))+1);
		Min(x, i);
		le[i] = x;
		while(top && q2[top]>=x){
			Min(le[i], le[q2[top]]);
			--top;
		}
		q2[++top] = i;
	}
	Handle(cnt);
	return sum;
}
void In(){
	sum = top = cnt = 0;
	cin >> n;
	For(i,1,n){
		int x, y;
		cin >> x >> y;
		++a[x].F;
		if(y) ++a[x].S;
	}
	sort(a+1,a+1+n);
	cout << Green() << ' ';
	sum = 0;
	cout << Grass() << '\n';
	For(i,1,n) a[i] = {0,0};
}
void Out(){
	int T;
	cin >> T;
	while(T--){
		In();
	}
}
int main(){
    ios_base::sync_with_stdio(NULL);
    cin .tie(NULL); cout .tie(NULL);
  //  freopen(".inp ", "r", stdin);
//    freopen(".out", "w", stdout);
	Out();
}


/**GreenGrass**/
#include <bits/stdc++.h>
#define Ford(i,a,b) for(int i=a;i>=b;--i)
#define Forr(i,a,b) for(int i=a;i<b;++i)
#define For(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
#define mp make_pair
#define S second
#define F first
using namespace std;
template <typename T> inline void Min(T &a, T b){if(a>b) a=b;}
template <typename T> inline void Max(T &a, T b){if(a<b) a=b;}
typedef vector <int> vi;
typedef pair <int,int> ii;
typedef vector <ii> vii;
typedef pair <ii,int> iii;
const int N = 200005;
ii a[N];
priority_queue <int> Q;
int sum, run, n, le[N], q[N], cnt, q2[N], top;
int Green(){
	run = N;
	Ford(i,n,1){
		Min(run, a[i].F);
		q[++cnt] = run;
		sum += run;
		--run;
		if(run<1) break;
	}
	For(i,1,cnt/2) swap(q[i], q[cnt-i+1]);
	return sum;
}
void Handle(int x){
	if(!x) return;
	int lef = lower_bound(a+1,a+1+n,mp(q[le[x]], 0))-a;
	int rig = upper_bound(a+1,a+1+n,mp(q[x], N))-a-1;
	while(Q.size()) Q.pop();
	For(i,lef,rig) Q.push(a[i].S);
	Ford(i,x,le[x]){
		sum += min(q[i],Q.top());
		Q.pop();
	}
	Handle(le[x]-1);
}
int Grass(){
	For(i,1,cnt){
		int x = max(1, i+1-(upper_bound(a+1,a+1+n,mp(q[i], N))-lower_bound(a+1,a+1+n,mp(q[i], 0))));
		Min(x, i);
		le[i] = x;
		while(top && q2[top]>=x){
			Min(le[i], le[q2[top]]);
			--top;
		}
		if(le[i]>1&&le[i]!=i) --le[i];
		q2[++top] = i;
	}
	Handle(cnt);
	return sum;
}
void In(){
	sum = top = cnt = 0;
	cin >> n;
	For(i,1,n){
		int x; bool y;
		cin >> x >> y;
		++a[x].F;
		if(y) ++a[x].S;
	}
	sort(a+1,a+1+n);
	cout << Green() << ' ';
	sum = 0;
	cout << Grass() << '\n';
	For(i,1,n) a[i] = {0,0};
}
void Out(){
	int T;
	cin >> T;
	while(T--){
		In();
	}
}
int main(){
    ios_base::sync_with_stdio(NULL);
    cin .tie(NULL); cout .tie(NULL);
  //  freopen(".inp ", "r", stdin);
//    freopen(".out", "w", stdout);
	Out();
}


/**GreenGrass**/
#include <bits/stdc++.h>
#define Ford(i,a,b) for(int i=a;i>=b;--i)
#define Forr(i,a,b) for(int i=a;i<b;++i)
#define For(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
#define mp make_pair
#define S second
#define F first
using namespace std;
template <typename T> inline void Min(T &a, T b){if(a>b) a=b;}
template <typename T> inline void Max(T &a, T b){if(a<b) a=b;}
typedef vector <int> vi;
typedef pair <int,int> ii;
typedef vector <ii> vii;
typedef pair <ii,int> iii;
const int N = 200005;
ii a[N];
priority_queue <int> Q;
int sum, run, n, le[N], q[N], cnt, q2[N], top;
int Green(){
	run = N;
	Ford(i,n,1){
		Min(run, a[i].F);
		q[++cnt] = run;
		sum += run;
		--run;
		if(run<1) break;
	}
	For(i,1,cnt/2) swap(q[i], q[cnt-i+1]);
	return sum;
}
void Handle(int x){
	if(!x) return;
	int lef = lower_bound(a+1,a+1+n,mp(q[le[x]], 0))-a;
	int rig = upper_bound(a+1,a+1+n,mp(q[x], N))-a-1;
	while(Q.size()) Q.pop();
	For(i,lef,rig) Q.push(a[i].S);
	Ford(i,x,le[x]){
		sum += min(q[i],Q.top());
		Q.pop();
	}
	Handle(le[x]-1);
}
int Grass(){
	For(i,1,cnt){
		int x = max(1, i-(upper_bound(a+1,a+1+n,mp(q[i], N))-lower_bound(a+1,a+1+n,mp(q[i], 0)))+1);
		Min(x, i);
		le[i] = x;
		while(top && q2[top]>=x){
			Min(le[i], le[q2[top]]);
			--top;
		}
		q2[++top] = i;
	}
	Handle(cnt);
	return sum;
}
void In(){
	sum = top = cnt = 0;
	cin >> n;
	For(i,1,n){
		int x, y;
		cin >> x >> y;
		++a[x].F;
		if(y) ++a[x].S;
	}
	sort(a+1,a+1+n);
	cout << Green() << ' ';
	sum = 0;
	cout << Grass() << '\n';
	For(i,1,n) a[i] = {0,0};
}
void Out(){
	int T;
	cin >> T;
	while(T--){
		In();
	}
}
int main(){
    ios_base::sync_with_stdio(NULL);
    cin .tie(NULL); cout .tie(NULL);
 //   freopen(".inp ", "r", stdin);
//    freopen(".out", "w", stdout);
	Out();
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pb push_back
#define F first
#define S second
#define ppp pair<ll,pair<ll,ll>>
#define pp pair<ll,ll>
#define all(x) (x).begin(),(x).end()
#define pi 3.14159265358
#define MOD 998244353

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

     ios_base::sync_with_stdio(false);
     cin.tie(NULL);

    ll t;

    cin>>t;

    while(t--)
    {
         ll n,i,j,k,x,f;

         cin>>n;

         map<ll,pp>mp;
         for(i=0;i<n;i++)
         {
            cin>>x>>f;
            mp[x].F++;
            mp[x].S+=f;
            //cout<<x<<" "<<mp[x].F<<" "<<mp[x].S<<endl;
         }

         vector<pp> v;

         for(auto tmp:mp)
         {
            v.pb({tmp.S.F,tmp.S.S});
         }

         sort(all(v));
         reverse(all(v));

         vector<ll> ans;

         ll ans1=0,ans2=0;

         for(auto it:v)
         {
            if((ll)ans.size()==0)
               {
                  ans.pb(it.F);
               }
               else
               {
                  ans.pb(min(it.F,ans[(ll)ans.size()-1]-1));
               }
         }

         //ans.pb(0);

         sort(all(ans));
         while(*(ans.begin())<=0)
         {
            ans.erase(ans.begin());
         }

         // sort(all(ans));
          reverse(all(ans));

         for(i=0;i<ans.size();i++)
            {
               //cout<<ans[i]<<" ";
               ans1+=ans[i];
            }
         //cout<<endl;
          ll idx=0;

         ll mx=0;

         // for(i=0;i<v.size();i++)
         // {
         //    cout<<v[i].F<<" "<<v[i].S<<endl;
         //    // if(v[i].F>=ans[idx])
         //    // {
         //    //    mx=max(v[i].S,mx);
         //    // }
         //    // else
         //    // {
         //    //    ans2+=mx;\o
         //    //    mx=0;
         //    //    idx++;
         //    // }
         // }

         priority_queue<pp> q;

         for(i=0;i<v.size();i++)
         {
            q.push({v[i].F,v[i].S});
         }

         ll yet=INT_MAX;

         ll cnt=0;

         while(!q.empty())
         {
            ll f,s;
            f=q.top().F;
            s=q.top().S;

           // cout<<f<<" "<<s<<" "<<cnt<<endl;
            q.pop();
            if(f<yet)
            {
               ans2+=s;
               yet=f;
               cnt++;
            }
            else
            {
               f--;
               s=min(f,s);
               if(f>0)
               q.push({f,s});
            }
            if(cnt==k)
               break;
         }

          cout<<ans1<<" "<<ans2<<endl;

    }

}


#include<bits/stdc++.h>
using namespace std;

typedef int ll;
#define pb push_back
#define F first
#define S second
#define ppp pair<ll,pair<ll,ll>>
#define pp pair<ll,ll>
#define all(x) (x).begin(),(x).end()
#define pi 3.14159265358
#define MOD 998244353

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

     ios_base::sync_with_stdio(false);
     cin.tie(NULL);

    ll t;

    cin>>t;

    while(t--)
    {
         ll n,i,j,k,x,f;

         cin>>n;

         map<ll,pp>mp;
         for(i=0;i<n;i++)
         {
            cin>>x>>f;
            mp[x].F++;
            mp[x].S+=f;
            //cout<<x<<" "<<mp[x].F<<" "<<mp[x].S<<endl;
         }

         vector<pp> v;

         for(auto tmp:mp)
         {
            v.pb({tmp.S.F,tmp.S.S});
         }

         sort(all(v));
         reverse(all(v));

         vector<ll> ans;

         ll ans1=0,ans2=0;

         // for(auto it:v)
         // {
         //    if((ll)ans.size()==0)
         //       {
         //          ans.pb(it.F);
         //       }
         //       else
         //       {
         //          ans.pb(min(it.F,ans[(ll)ans.size()-1]-1));
         //       }
         // }

         //ans.pb(0);

         // sort(all(ans));
         // while(*(ans.begin())<=0)
         // {
         //    ans.erase(ans.begin());
         // }

         // // sort(all(ans));
         //  reverse(all(ans));

         // for(i=0;i<ans.size();i++)
         //    {
         //       //cout<<ans[i]<<" ";
         //       ans1+=ans[i];
         //    }
         //cout<<endl;
          ll idx=0;

         ll mx=0;

         // for(i=0;i<v.size();i++)
         // {
         //    cout<<v[i].F<<" "<<v[i].S<<endl;
         //    // if(v[i].F>=ans[idx])
         //    // {
         //    //    mx=max(v[i].S,mx);
         //    // }
         //    // else
         //    // {
         //    //    ans2+=mx;\o
         //    //    mx=0;
         //    //    idx++;
         //    // }
         // }

         priority_queue<pp> q;

         for(i=0;i<v.size();i++)
         {
            q.push({v[i].F,v[i].S});
         }

         ll yet=INT_MAX;

         ll cnt=0;

         while(!q.empty())
         {
            ll f,s;
            f=q.top().F;
            s=q.top().S;

          // cout<<f<<" "<<s<<" "<<cnt<<endl;
            q.pop();
            if(f<yet)
            {
               ans2+=s;
               ans1+=f;
               yet=f;
               cnt++;
            }
            else
            {
               f=yet-1;
               s=min(f,s);
               // f--;
               // s=min(f,s);
               if(f>0 && s>0)
               q.push({f,s});
            }
            // if(cnt==k)
            //    break;
         }

          cout<<ans1<<" "<<ans2<<endl;

    }

}


#include<bits/stdc++.h>
using namespace std;

typedef int ll;
#define pb push_back
#define F first
#define S second
#define ppp pair<ll,pair<ll,ll>>
#define pp pair<ll,ll>
#define all(x) (x).begin(),(x).end()
#define pi 3.14159265358
#define MOD 998244353

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

     ios_base::sync_with_stdio(false);
     cin.tie(NULL);

    ll t;

    cin>>t;

    while(t--)
    {
         ll n,i,j,k,x,f;

         cin>>n;

         map<ll,pp>mp;
         for(i=0;i<n;i++)
         {
            cin>>x>>f;
            mp[x].F++;
            mp[x].S+=f;
            //cout<<x<<" "<<mp[x].F<<" "<<mp[x].S<<endl;
         }

         vector<pp> v;

         for(auto tmp:mp)
         {
            v.pb({tmp.S.F,tmp.S.S});
         }

         sort(all(v));
         reverse(all(v));

         vector<ll> ans;

         ll ans1=0,ans2=0;

         for(auto it:v)
         {
            if((ll)ans.size()==0)
               {
                  ans.pb(it.F);
               }
               else
               {
                  ans.pb(min(it.F,ans[(ll)ans.size()-1]-1));
               }
         }

         //ans.pb(0);

         sort(all(ans));
         while(*(ans.begin())<=0)
         {
            ans.erase(ans.begin());
         }

         // sort(all(ans));
          reverse(all(ans));

         for(i=0;i<ans.size();i++)
            {
               //cout<<ans[i]<<" ";
               ans1+=ans[i];
            }
         //cout<<endl;
          ll idx=0;

         ll mx=0;

         // for(i=0;i<v.size();i++)
         // {
         //    cout<<v[i].F<<" "<<v[i].S<<endl;
         //    // if(v[i].F>=ans[idx])
         //    // {
         //    //    mx=max(v[i].S,mx);
         //    // }
         //    // else
         //    // {
         //    //    ans2+=mx;\o
         //    //    mx=0;
         //    //    idx++;
         //    // }
         // }

         priority_queue<pp> q;

         for(i=0;i<v.size();i++)
         {
            q.push({v[i].F,v[i].S});
         }

         ll yet=INT_MAX;

         ll cnt=0;

         while(!q.empty())
         {
            ll f,s;
            f=q.top().F;
            s=q.top().S;

          // cout<<f<<" "<<s<<" "<<cnt<<endl;
            q.pop();
            if(f<yet)
            {
               ans2+=s;
               yet=f;
               cnt++;
            }
            else
            {
               f=yet-1;
               s=min(f,s);
               // f--;
               // s=min(f,s);
               if(f>0 && s>0)
               q.push({f,s});
            }
            if(cnt==k)
               break;
         }

          cout<<ans1<<" "<<ans2<<endl;

    }

}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pb push_back
#define F first
#define S second
#define ppp pair<ll,pair<ll,ll>>
#define pp pair<ll,ll>
#define all(x) (x).begin(),(x).end()
#define pi 3.14159265358
#define MOD 998244353

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

     ios_base::sync_with_stdio(false);
     cin.tie(NULL);

    ll t;

    cin>>t;

    while(t--)
    {
         ll n,i,j,k,x,f;

         cin>>n;

         map<ll,pp>mp;
         for(i=0;i<n;i++)
         {
            cin>>x>>f;
            mp[x].F++;
            mp[x].S+=f;
            //cout<<x<<" "<<mp[x].F<<" "<<mp[x].S<<endl;
         }

         vector<pp> v;

         for(auto tmp:mp)
         {
            v.pb({tmp.S.F,tmp.S.S});
         }

         sort(all(v));
         reverse(all(v));

         vector<ll> ans;

         ll ans1=0,ans2=0;

         for(auto it:v)
         {
            if((ll)ans.size()==0)
               {
                  ans.pb(it.F);
               }
               else
               {
                  ans.pb(min(it.F,ans[(ll)ans.size()-1]-1));
               }
         }

         //ans.pb(0);

         sort(all(ans));
         while(*(ans.begin())<=0)
         {
            ans.erase(ans.begin());
         }

         // sort(all(ans));
          reverse(all(ans));

         for(i=0;i<ans.size();i++)
            {
               //cout<<ans[i]<<" ";
               ans1+=ans[i];
            }
         //cout<<endl;
          ll idx=0;

         ll mx=0;

         // for(i=0;i<v.size();i++)
         // {
         //    cout<<v[i].F<<" "<<v[i].S<<endl;
         //    // if(v[i].F>=ans[idx])
         //    // {
         //    //    mx=max(v[i].S,mx);
         //    // }
         //    // else
         //    // {
         //    //    ans2+=mx;\o
         //    //    mx=0;
         //    //    idx++;
         //    // }
         // }

         priority_queue<pp> q;

         for(i=0;i<v.size();i++)
         {
            q.push({v[i].F,v[i].S});
         }

         ll yet=INT_MAX;

         ll cnt=0;

         while(!q.empty())
         {
            ll f,s;
            f=q.top().F;
            s=q.top().S;

          // cout<<f<<" "<<s<<" "<<cnt<<endl;
            q.pop();
            if(f<yet)
            {
               ans2+=s;
               yet=f;
               cnt++;
            }
            else
            {
               f=yet-1;
               s=min(f,s);
               // f--;
               // s=min(f,s);
               if(f>0 && s>0)
               q.push({f,s});
            }
            if(cnt==k)
               break;
         }

          cout<<ans1<<" "<<ans2<<endl;

    }

}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pb push_back
#define F first
#define S second
#define ppp pair<ll,pair<ll,ll>>
#define pp pair<ll,ll>
#define all(x) (x).begin(),(x).end()
#define pi 3.14159265358
#define MOD 998244353

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

     ios_base::sync_with_stdio(false);
     cin.tie(NULL);

    ll t;

    cin>>t;

    while(t--)
    {
         ll n,i,j,k,x,f;

         cin>>n;

         map<ll,pp>mp;
         for(i=0;i<n;i++)
         {
            cin>>x>>f;
            mp[x].F++;
            mp[x].S+=f;
            //cout<<x<<" "<<mp[x].F<<" "<<mp[x].S<<endl;
         }

         vector<pp> v;

         for(auto tmp:mp)
         {
            v.pb({tmp.S.F,tmp.S.S});
         }

         sort(all(v));
         reverse(all(v));

         vector<ll> ans;

         ll ans1=0,ans2=0;

         for(auto it:v)
         {
            if((ll)ans.size()==0)
               {
                  ans.pb(it.F);
               }
               else
               {
                  ans.pb(min(it.F,ans[(ll)ans.size()-1]-1));
               }
         }

         //ans.pb(0);

         sort(all(ans));
         while(*(ans.begin())<=0)
         {
            ans.erase(ans.begin());
         }

         // sort(all(ans));
          reverse(all(ans));

         for(i=0;i<ans.size();i++)
            {
               //cout<<ans[i]<<" ";
               ans1+=ans[i];
            }
         //cout<<endl;
          ll idx=0;

         ll mx=0;

         // for(i=0;i<v.size();i++)
         // {
         //    cout<<v[i].F<<" "<<v[i].S<<endl;
         //    // if(v[i].F>=ans[idx])
         //    // {
         //    //    mx=max(v[i].S,mx);
         //    // }
         //    // else
         //    // {
         //    //    ans2+=mx;\o
         //    //    mx=0;
         //    //    idx++;
         //    // }
         // }

         priority_queue<pp> q;

         for(i=0;i<v.size();i++)
         {
            q.push({v[i].F,v[i].S});
         }

         ll yet=INT_MAX;

         ll cnt=0;

         while(!q.empty() && cnt<k)
         {
            ll f,s;
            f=q.top().F;
            s=q.top().S;

           // cout<<f<<" "<<s<<" "<<cnt<<endl;
            q.pop();
            if(f<yet)
            {
               ans2+=s;
               yet=f;
               cnt++;
            }
            else
            {
               f--;
               s=min(f,s);
               if(f>0)
               q.push({f,s});
            }
         }

          cout<<ans1<<" "<<ans2<<endl;

    }

}


#include<bits/stdc++.h>
using namespace std;

typedef int ll;
#define pb push_back
#define F first
#define S second
#define ppp pair<ll,pair<ll,ll>>
#define pp pair<ll,ll>
#define all(x) (x).begin(),(x).end()
#define pi 3.14159265358
#define MOD 998244353

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

     ios_base::sync_with_stdio(false);
     cin.tie(NULL);

    ll t;

    cin>>t;

    while(t--)
    {
         ll n,i,j,k,x,f;

         cin>>n;

         map<ll,pp>mp;
         for(i=0;i<n;i++)
         {
            cin>>x>>f;
            mp[x].F++;
            mp[x].S+=f;
            //cout<<x<<" "<<mp[x].F<<" "<<mp[x].S<<endl;
         }

         vector<pp> v;

         for(auto tmp:mp)
         {
            v.pb({tmp.S.F,tmp.S.S});
         }

         sort(all(v));
         reverse(all(v));

         vector<ll> ans;

         ll ans1=0,ans2=0;

         // for(auto it:v)
         // {
         //    if((ll)ans.size()==0)
         //       {
         //          ans.pb(it.F);
         //       }
         //       else
         //       {
         //          ans.pb(min(it.F,ans[(ll)ans.size()-1]-1));
         //       }
         // }

         //ans.pb(0);

         // sort(all(ans));
         // while(*(ans.begin())<=0)
         // {
         //    ans.erase(ans.begin());
         // }

         // // sort(all(ans));
         //  reverse(all(ans));

         // for(i=0;i<ans.size();i++)
         //    {
         //       //cout<<ans[i]<<" ";
         //       ans1+=ans[i];
         //    }
         //cout<<endl;
          ll idx=0;

         ll mx=0;

         // for(i=0;i<v.size();i++)
         // {
         //    cout<<v[i].F<<" "<<v[i].S<<endl;
         //    // if(v[i].F>=ans[idx])
         //    // {
         //    //    mx=max(v[i].S,mx);
         //    // }
         //    // else
         //    // {
         //    //    ans2+=mx;\o
         //    //    mx=0;
         //    //    idx++;
         //    // }
         // }

         priority_queue<pp> q;

         for(i=0;i<v.size();i++)
         {
            q.push({v[i].F,v[i].S});
         }

         ll yet=INT_MAX;

         ll cnt=0;

         while(!q.empty())
         {
            ll f,s;
            f=q.top().F;
            s=q.top().S;

          // cout<<f<<" "<<s<<" "<<cnt<<endl;
            q.pop();
            if(f<yet)
            {
               ans2+=s;
               ans1+=f;
               yet=f;
               cnt++;
            }
            else
            {
               f=yet-1;
               s=min(f,s);
               // f--;
               // s=min(f,s);
               if(f>0)
               q.push({f,s});
            }
            // if(cnt==k)
            //    break;
         }

          cout<<ans1<<" "<<ans2<<endl;

    }

}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pb push_back
#define F first
#define S second
#define ppp pair<ll,pair<ll,ll>>
#define pp pair<ll,ll>
#define all(x) (x).begin(),(x).end()
#define pi 3.14159265358
#define MOD 998244353

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

     ios_base::sync_with_stdio(false);
     cin.tie(NULL);

    ll t;

    cin>>t;

    while(t--)
    {
         ll n,i,j,k,x,f;

         cin>>n;

         map<ll,pp>mp;
         for(i=0;i<n;i++)
         {
            cin>>x>>f;
            mp[x].F++;
            mp[x].S+=f;
            //cout<<x<<" "<<mp[x].F<<" "<<mp[x].S<<endl;
         }

         vector<pp> v;

         for(auto tmp:mp)
         {
            v.pb({tmp.S.F,tmp.S.S});
         }

         sort(all(v));
         reverse(all(v));

         vector<ll> ans;

         ll ans1=0,ans2=0;

         for(auto it:v)
         {
            if((ll)ans.size()==0)
               {
                  ans.pb(it.F);
               }
               else
               {
                  ans.pb(min(it.F,ans[(ll)ans.size()-1]-1));
               }
         }

         //ans.pb(0);

         sort(all(ans));
         while(*(ans.begin())<=0)
         {
            ans.erase(ans.begin());
         }

         // sort(all(ans));
          reverse(all(ans));

         for(i=0;i<ans.size();i++)
            {
               //cout<<ans[i]<<" ";
               ans1+=ans[i];
            }
         //cout<<endl;
          ll idx=0;

         ll mx=0;

         // for(i=0;i<v.size();i++)
         // {
         //    cout<<v[i].F<<" "<<v[i].S<<endl;
         //    // if(v[i].F>=ans[idx])
         //    // {
         //    //    mx=max(v[i].S,mx);
         //    // }
         //    // else
         //    // {
         //    //    ans2+=mx;\o
         //    //    mx=0;
         //    //    idx++;
         //    // }
         // }

         priority_queue<pp> q;

         for(i=0;i<v.size();i++)
         {
            q.push({v[i].F,v[i].S});
         }

         ll yet=INT_MAX;

         ll cnt=0;

         while(!q.empty())
         {
            ll f,s;
            f=q.top().F;
            s=q.top().S;

           // cout<<f<<" "<<s<<" "<<cnt<<endl;
            q.pop();
            if(f<yet)
            {
               ans2+=s;
               yet=f;
               cnt++;
            }
            else
            {
               f--;
               s=min(f,s);
               if(f>0 && s>0)
               q.push({f,s});
            }
            if(cnt==k)
               break;
         }

          cout<<ans1<<" "<<ans2<<endl;

    }

}


#include<bits/stdc++.h>
using namespace std;

typedef int ll;
#define pb push_back
#define F first
#define S second
#define ppp pair<ll,pair<ll,ll>>
#define pp pair<ll,ll>
#define all(x) (x).begin(),(x).end()
#define pi 3.14159265358
#define MOD 998244353

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

     ios_base::sync_with_stdio(false);
     cin.tie(NULL);

    ll t;

    cin>>t;

    while(t--)
    {
         ll n,i,j,k,x,f;

         cin>>n;

         map<ll,pp>mp;
         for(i=0;i<n;i++)
         {
            cin>>x>>f;
            mp[x].F++;
            mp[x].S+=f;
            //cout<<x<<" "<<mp[x].F<<" "<<mp[x].S<<endl;
         }

         vector<pp> v;

         for(auto tmp:mp)
         {
            v.pb({tmp.S.F,tmp.S.S});
         }

         sort(all(v));
         reverse(all(v));

         vector<ll> ans;

         ll ans1=0,ans2=0;

         // for(auto it:v)
         // {
         //    if((ll)ans.size()==0)
         //       {
         //          ans.pb(it.F);
         //       }
         //       else
         //       {
         //          ans.pb(min(it.F,ans[(ll)ans.size()-1]-1));
         //       }
         // }

         //ans.pb(0);

         // sort(all(ans));
         // while(*(ans.begin())<=0)
         // {
         //    ans.erase(ans.begin());
         // }

         // // sort(all(ans));
         //  reverse(all(ans));

         // for(i=0;i<ans.size();i++)
         //    {
         //       //cout<<ans[i]<<" ";
         //       ans1+=ans[i];
         //    }
         //cout<<endl;
          ll idx=0;

         ll mx=0;

         // for(i=0;i<v.size();i++)
         // {
         //    cout<<v[i].F<<" "<<v[i].S<<endl;
         //    // if(v[i].F>=ans[idx])
         //    // {
         //    //    mx=max(v[i].S,mx);
         //    // }
         //    // else
         //    // {
         //    //    ans2+=mx;\o
         //    //    mx=0;
         //    //    idx++;
         //    // }
         // }

         priority_queue<pp> q;

         for(i=0;i<v.size();i++)
         {
            q.push({v[i].F,v[i].S});
         }

         ll yet=INT_MAX;

         ll cnt=0;

         while(!q.empty())
         {
            ll f,s;
            f=q.top().F;
            s=q.top().S;

          // cout<<f<<" "<<s<<" "<<cnt<<endl;
            q.pop();
            if(f<yet)
            {
               ans2+=s;
               ans1+=f;
               yet=f;
               cnt++;
            }
            else
            {
               f=yet-1;
               s=min(f,s);
               // f--;
               // s=min(f,s);
               if(f>0 && s>0)
               q.push({f,s});
            }
            if(cnt==k)
               break;
         }

          cout<<ans1<<" "<<ans2<<endl;

    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t,i;
    cin>>t;
    while(t--){
        ll n,p,p2;
        cin>>n;
        map<ll,pair<ll,ll>>m;

        for(i=0;i<n;++i){
            cin>>p;
            m[p].first++;
            cin>>p2;
            if(p2==1)
                m[p].second++;
        }
        std::vector<pair<ll,ll>> vec;
        vec.push_back({0,0});

        for(auto it=m.begin();it!=m.end();++it){
                vec.push_back(it->second);
        }

        pair<ll,ll>r,q;
        sort(vec.begin(),vec.end());

        for(i=vec.size()-2;i>=0;--i){

            if(vec[i].first==vec[i+1].first){
                r=vec[i];
                q=vec[i+1];
                vec[i]=min(r,q);
                vec[i+1]=max(r,q);
                vec[i].first--;
                vec[i].second=min(vec[i].first,vec[i].second);
            }
        }
        sort(vec.begin(),vec.end());

        ll sum=vec[vec.size()-1].first,sum2=vec[vec.size()-1].second,k=vec[vec.size()-1].first;

        for(i=vec.size()-2;i>=0;--i){

            if(vec[i].first>=k){
                sum+=k-1;
                sum2+=min(k-1,vec[i].second);
                k=k-1;
            }
            else{ sum+=vec[i].first;sum2+=vec[i].second;k=vec[i].first;}
            if(k<=0)break;
        }
        cout<<sum<<" ";
        cout<<sum2<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

int main(){

    int t,i;
    scanf("%d",&t);
    while(t--){
        int n,p,p2;
        scanf("%d",&n);
        map<int,int>m,m2;

        for(i=0;i<n;++i){
            scanf("%d",&p);
            --p;
            m[p]++;
            scanf("%d",&p2);
            if(p2==1)
                m2[p]++;
        }
        std::vector<vector<int>> v3(n+1);

        for(auto it=m.begin();it!=m.end();++it){
            v3[it->second].push_back(m2[it->first]);
        }
        multiset<int>s;
        ll sum=0,sum2=0;

        for(i=n;i>=0;--i){
            for(auto a:v3[i])
               { s.insert(a);}
            if(!s.empty()){
                int k=*s.rbegin();
                sum+=i;
                sum2+=min(i,k);
                auto it=s.end();
                --it;
                s.erase(it);
            }
        }
        printf("%Ld ",sum);
        printf("%Ld\n",sum2);
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t,i;
    cin>>t;
    while(t--){
        ll n,p,p2;
        cin>>n;
        map<ll,pair<ll,ll>>m;

        for(i=0;i<n;++i){
            cin>>p;
            m[p].first++;
            cin>>p2;
            if(p2==1)
                m[p].second++;
        }
        std::vector<pair<ll,ll>> vec;
        vec.push_back({0,0});

        for(auto it=m.begin();it!=m.end();++it){
                vec.push_back(it->second);
        }

        sort(vec.begin(),vec.end());

        for(i=vec.size()-2;i>=0;--i){

            if(vec[i].first>=vec[i+1].first){
                vec[i].first=vec[i+1].first;
                vec[i].second=min(vec[i].first,vec[i].second);

                p=vec[i].second;
                p2=vec[i+1].second;
                vec[i].second=min(p,p2);
                vec[i+1].second=max(p,p2);
                vec[i].first--;
                vec[i].second=min(vec[i].first,vec[i].second);
            }
        }
        sort(vec.begin(),vec.end());

        ll sum=vec[vec.size()-1].first,sum2=vec[vec.size()-1].second,k=vec[vec.size()-1].first;

        for(i=vec.size()-2;i>=0;--i){

            if(vec[i].first>=k){
                sum+=k-1;
                sum2+=min(k-1,vec[i].second);
                k=k-1;
            }
            else{ sum+=vec[i].first;sum2+=vec[i].second;k=vec[i].first;}
            if(k<=0)break;
        }
        cout<<sum<<" ";
        cout<<sum2<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t,i;
    cin>>t;
    while(t--){
        ll n,p,p2;
        cin>>n;
        std::vector<int> v(200001),v2(200001);

        for(i=0;i<n;++i){
            cin>>p;
            --p;
            v[p]++;
            cin>>p2;
            if(p2==1)
                v2[p]++;
        }
        std::vector<vector<int>> v3(n+1);

        for(i=0;i<=200000;++i){
            v3[v[i]].push_back(v2[i]);
        }
        multiset<int>s;
        ll sum=0,sum2=0;

        for(i=n;i>=0;--i){
            for(auto a:v3[i])
               { s.insert(a);}
            if(!s.empty()){
                int k=*s.rbegin();
                sum+=i;
                sum2+=min(i,k);
                auto it=s.end();
                --it;
                s.erase(it);
            }
        }
        cout<<sum<<" "<<sum2<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t,i;
    cin>>t;
    while(t--){
        ll n,p,p2;
        cin>>n;
        map<ll,pair<ll,ll>>m;

        for(i=0;i<n;++i){
            cin>>p;
            m[p].first++;
            cin>>p2;
            if(p2==1)
                m[p].second++;
        }
        std::vector<pair<ll,ll>> vec;

        for(auto it=m.begin();it!=m.end();++it){
                vec.push_back(it->second);
        }

        sort(vec.begin(),vec.end());

        for(i=vec.size()-2;i>=0;--i){

            if(vec[i].first>=vec[i+1].first){
                vec[i].first=vec[i+1].first;
                vec[i].second=min(vec[i].first,vec[i].second);

                p=vec[i].second;
                p2=vec[i+1].second;
                vec[i].second=min(p,p2);
                vec[i+1].second=max(p,p2);
                vec[i].first--;
                vec[i].second=min(vec[i].first,vec[i].second);
            }
        }
        /*sort(vec.begin(),vec.end());

        ll sum=vec[vec.size()-1].first,sum2=vec[vec.size()-1].second,k=vec[vec.size()-1].first;

        for(i=vec.size()-2;i>=0;--i){

            if(vec[i].first>=k){
                sum+=k-1;
                sum2+=min(k-1,vec[i].second);
                k=k-1;
            }
            else{ sum+=vec[i].first;sum2+=vec[i].second;k=vec[i].first;}
            if(k<=0)break;
        }*/
        ll sum=0,sum2=0;
        for(i=0;i<vec.size();++i){
            if(vec[i].first<=0)
                continue;
            sum+=vec[i].first;
            sum2+=vec[i].second;
        }
        cout<<sum<<" ";
        cout<<sum2<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

int main(){

    int t,i;
    scanf("%d",&t);
    while(t--){
        int n,p,p2;
        scanf("%d",&n);
        map<int,int>m;
        int m2[200001]={0};
        for(i=0;i<n;++i){
            scanf("%d",&p);
            --p;
            m[p]++;
            scanf("%d",&p2);
            if(p2==1)
                m2[p]++;
        }
        std::vector<vector<int>> v3(n+1);

        for(auto it=m.begin();it!=m.end();++it){
            v3[it->second].push_back(m2[it->first]);
        }
        multiset<int>s;
        ll sum=0,sum2=0;

        for(i=n;i>=0;--i){
            for(auto a:v3[i])
               { s.insert(a);}
            if(!s.empty()){
                int k=*s.rbegin();
                sum+=i;
                sum2+=min(i,k);
                auto it=s.end();
                --it;
                s.erase(it);
            }
        }
        printf("%Ld ",sum);
        printf("%Ld\n",sum2);
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t,i;
    cin>>t;
    while(t--){
        ll n,p,p2;
        cin>>n;
        map<ll,pair<ll,ll>>m;

        for(i=0;i<n;++i){
            cin>>p;
            m[p].first++;
            cin>>p2;
            if(p2==1)
                m[p].second++;
        }
        std::vector<pair<ll,ll>> vec;
        vec.push_back({0,0});

        for(auto it=m.begin();it!=m.end();++it){
                vec.push_back(it->second);
        }

        sort(vec.begin(),vec.end());
        ll k=vec[vec.size()-1].first;
        for(i=vec.size()-2;i>=0;--i){

            if(vec[i].first>=k){
                vec[i].first=k-1;
                vec[i].second=min(k-1,vec[i].second);
                k=k-1;
            }
            else{k=vec[i].first;}
        }
        sort(vec.begin(),vec.end());

        ll sum=vec[vec.size()-1].first,sum2=vec[vec.size()-1].second;
        k=vec[vec.size()-1].first;

        for(i=vec.size()-2;i>=0;--i){

            if(vec[i].first>=k){
                sum+=k-1;
                sum2+=min(k-1,vec[i].second);
                k=k-1;
            }
            else{ sum+=vec[i].first;sum2+=vec[i].second;k=vec[i].first;}
            if(k<=0)break;
        }
        cout<<sum<<" ";
        cout<<sum2<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t,i,j;
    cin>>t;
    while(t--){
        ll n,p,p2;
        cin>>n;
        map<ll,pair<ll,ll>>m;

        for(i=0;i<n;++i){
            cin>>p;
            m[p].first++;
            cin>>p2;
            if(p2==1)
                m[p].second++;
        }
        std::vector<pair<ll,ll>> vec;

        for(auto it=m.begin();it!=m.end();++it){
                vec.push_back(it->second);
        }

        sort(vec.begin(),vec.end());
        map<ll,ll>m2;
        for(i=vec.size()-2;i>=0;--i){

            if(vec[i].first>=vec[i+1].first){
                if(m2[vec[i].first]!=0){
                    j=m2[vec[i].first];
                    p=vec[j].second;
                    p2=vec[i].second;
                    vec[j].second=max(p,p2);
                    vec[i].second=min(p,p2);
                vec[i].second=min(vec[i].first,vec[i].second);
                vec[j].second=min(vec[j].first,vec[j].second);
                }
                vec[i].first=vec[i+1].first;
                vec[i].second=min(vec[i].first,vec[i].second);
                p=vec[i].second;
                p2=vec[i+1].second;
                vec[i].second=min(p,p2);
                vec[i+1].second=max(p,p2);
                vec[i].first--;
                vec[i].second=min(vec[i].first,vec[i].second);
            }
            if(m2[vec[i].first]==0)
                m2[vec[i].first]=i;
        }

        ll sum=0,sum2=0;
        for(i=0;i<vec.size();++i){
            if(vec[i].first<=0)
                continue;
            sum+=vec[i].first;
            sum2+=vec[i].second;
        }
        cout<<sum<<" ";
        cout<<sum2<<"\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007

int main(){

    int t,i;
    scanf("%d",&t);
    while(t--){
        int n,p,p2;
        scanf("%d",&n);
        map<int,int>m,m2;

        for(i=0;i<n;++i){
            scanf("%d",&p);
            --p;
            m[p]++;
            scanf("%d",&p2);
            if(p2==1)
                m2[p]++;
        }
        std::vector<vector<int>> v3(n+1);

        for(auto it=m.begin();it!=m.end();++it){
            v3[it->second].push_back(m2[it->first]);
        }
        multiset<int>s;
        ll sum=0,sum2=0;

        for(i=n;i>=0;--i){
            for(auto a:v3[i])
               { s.insert(a);}
            if(!s.empty()){
                int k=*s.rbegin();
                sum+=i;
                sum2+=min(i,k);
                auto it=s.end();
                --it;
                s.erase(it);
            }
        }
        printf("%Ld %Ld\n",sum,sum2);
    }
}


#include <bits/stdc++.h>

using namespace std;

#define ss second
#define ff first
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define rep(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define debug(x) cout << #x << " = " << x << endl
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using ll = long long;
using ii = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<pair<int,int>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3fLL;
const int maxn = 2*1e5+10;

int main(int argc,const char** argv){

	fastio;

	int tc;

	cin >> tc;

	while(tc--){

		int n,ans=0,max_f = 0;

		cin >> n;

		vi v(n),aux;
		vii cont(maxn,ii(0,0));

		rep(i,0,n){
			int x;
			cin >> v[i] >> x;
			cont[v[i]].ss++;
			cont[v[i]].ff+=x;
			max_f+=x;
		}

		sort(cont.rbegin(),cont.rend());

		set<int> max_p;

		rep(i,0,n){
			if(cont[i].ss == 0) break;
			for(int j = cont[i].ss; j >= 1; --j){
				if(!max_p.count(j)){
					max_p.insert(j);
					ans+=j;
					aux.pb(j);
					break;
				}
			}
		}

		for(int i=0,j=0;i<n;++i){
			if(cont[i].ff == 0) break;

		}

		cout << ans << ' ' << min(max_f,ans) << '\n';
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ss second
#define ff first
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define rep(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define debug(x) cout << #x << " = " << x << endl
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using ll = long long;
using ii = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<pair<int,int>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3fLL;
const int maxn = 2*1e5+10;

int main(int argc,const char** argv){

	fastio;

	int tc;

	cin >> tc;

	while(tc--){

		int n,ans=0,max_f = 0;

		cin >> n;

		vi v(n),cont(maxn,0);

		rep(i,0,n){
			int x;
			cin >> v[i] >> x;
			cont[v[i]]++;
			max_f+=x;
		}

		sort(cont.rbegin(),cont.rend());

		set<int> max_p;

		rep(i,0,n){
			//debug(cont[i]);
			//if(cont[i] >= max_p.size()) break;
			for(int j = cont[i]; j >= 1; --j){
				if(!max_p.count(j)){
					max_p.insert(j);
					ans+=j;
					break;
				}
			}
		}

		cout << ans << ' ' << min(max_f,ans) << '\n';
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ss second
#define ff first
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define rep(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define debug(x) cout << #x << " = " << x << endl
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using ll = long long;
using ii = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<pair<int,int>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3fLL;
const int maxn = 1e6+10;

int main(){

	int q;

	scanf("%d",&q);

	while(q--){
		int n,ans=0,max_f=0;
		scanf("%d",&n);
		vii v(n+1,ii(0,0));
		rep(i,0,n){
			int a,x;
			scanf("%d %d",&a,&x);
			v[a].ss++;
			v[a].ff+=x;
		}
		sort(v.rbegin(),v.rend());
		set<int> s;
		for(auto &x:v){
			if(!s.count(x.ss)){
				ans+=x.ss;
				s.insert(x.ss);
				max_f+=x.ff;
			}
			else{
				//if(s.size() == x.ss) continue;
				for(int i=x.ss-1;i>=1;i--){
					if(!s.count(i)){
						ans+=i;
						max_f+=min(i,x.ff);
						s.insert(i);
						break;
					}
				}
			}
		}
		printf("%d %d\n",ans,max_f);
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ss second
#define ff first
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define rep(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define debug(x) cout << #x << " = " << x << endl
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using ll = long long;
using ii = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<pair<int,int>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3fLL;
const int maxn = 2*1e5+10;

int main(int argc,const char** argv){

	fastio;

	int tc;

	cin >> tc;

	while(tc--){

		int n,ans=0,max_f = 0;

		cin >> n;

		vi v(n),aux;
		vii cont(n+1,ii(0,0));

		rep(i,0,n){
			int x;
			cin >> v[i] >> x;
			cont[v[i]].ss++;
			cont[v[i]].ff+=x;
		}

		sort(cont.rbegin(),cont.rend());

		set<int> max_p;

		rep(i,0,n){
			if(cont[i].ss == 0) break;
			for(int j = cont[i].ss; j >= 1; --j){
				if(!max_p.count(j)){
					max_p.insert(j);
					ans+=j;
					aux.pb(j);
					max_f+=min(cont[i].ff,j);
					break;
				}
			}
		}

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

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ss second
#define ff first
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define rep(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define debug(x) cout << #x << " = " << x << endl
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using ll = long long;
using ii = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<pair<int,int>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3fLL;
const int maxn = 2*1e5+10;

int main(int argc,const char** argv){

	fastio;

	int tc;

	cin >> tc;

	while(tc--){

		int n,ans=0,max_f = 0;

		cin >> n;

		vi v(n),aux;
		vii cont(maxn,ii(0,0));

		rep(i,0,n){
			int x;
			cin >> v[i] >> x;
			cont[v[i]].ss++;
			cont[v[i]].ff+=x;
		}

		sort(cont.rbegin(),cont.rend());

		set<int> max_p;

		rep(i,0,n){
			if(cont[i].ss == 0) break;
			for(int j = cont[i].ss; j >= 1; --j){
				if(!max_p.count(j)){
					max_p.insert(j);
					ans+=j;
					aux.pb(j);
					max_f+=min(cont[i].ff,j);
					break;
				}
			}
		}

		for(int i=0,j=0;i<n;++i){
			if(cont[i].ff == 0) break;

		}

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

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ss second
#define ff first
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define rep(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define debug(x) cout << #x << " = " << x << endl
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using ll = long long;
using ii = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<pair<int,int>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3fLL;
const int maxn = 2*1e5+10;

int main(int argc,const char** argv){

	fastio;

	int tc;

	cin >> tc;

	while(tc--){

		int n,ans=0,max_f = 0;

		cin >> n;

		vi v(n),aux;
		vii cont(maxn,ii(0,0));

		rep(i,0,n){
			int x;
			cin >> v[i] >> x;
			cont[v[i]].ss++;
			cont[v[i]].ff+=x;
		}

		sort(cont.rbegin(),cont.rend());

		set<int> max_p;

		rep(i,0,n){
			if(cont[i].ss == 0) break;
			for(int j = cont[i].ss; j >= 1; --j){
				if(!max_p.count(j)){
					max_p.insert(j);
					ans+=j;
					aux.pb(j);
					max_f+=min(cont[i].ff,j);
					break;
				}
			}
		}

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

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ss second
#define ff first
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define rep(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define debug(x) cout << #x << " = " << x << endl
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using ll = long long;
using ii = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<pair<int,int>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3fLL;
const int maxn = 1e6+10;

int main(){

	int q;

	scanf("%d",&q);

	while(q--){
		int n,ans=0,max_f=0;
		scanf("%d",&n);
		vii v(n+1,ii(0,0));
		rep(i,0,n){
			int a,x;
			scanf("%d %d",&a,&x);
			v[a].ss++;
			v[a].ff+=x;
		}
		sort(v.rbegin(),v.rend());
		set<int> s;
		for(auto &x:v){
			if(!s.count(x.ss)){
				ans+=x.ss;
				s.insert(x.ss);
				max_f+=x.ff;
			}
			else{
				if(s.size() == x.ss) continue;
				for(int i=x.ss-1;i>=1;i--){
					if(!s.count(i)){
						ans+=i;
						max_f+=min(i,x.ff);
						s.insert(i);
						break;
					}
				}
			}
		}
		printf("%d %d\n",ans,max_f);
	}

	return 0;
}


///  Code by Leonardo16

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("avx2")

#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================
int n;

bool cmp(pair<int,ii>a, pair<int,ii>b){
    return a.scd.fst<b.scd.fst;
}

vi vv;
map<int,ii>mp;

void solve(){
    cin>>n;
    mp.clear();

    for(int i=1;i<=n;i++){
        mp[i].fst=0;
        mp[i].scd=0;
    }

    for(int i=0;i<n;i++){
        int x,v;
        cin>>x>>v;
        mp[x].scd+=v;
        mp[x].fst++;
    }
    vi del;
    for(int i=1;i<=n;i++){
        if(mp[i].fst==0){
            del.pb(i);
        }
    }
    for(auto it:del){
        mp.erase(it);
    }

    vector<pair<int,ii>>v;

    vv.clear();
    for(auto it:mp){
        vv.pb(it.scd.fst);
    }
    sort(all(vv));

    vector<int>take;
    take.clear();
    int ans=0;
    take.pb(vv[vv.sz()-1]);

    int wr=vv.sz();
    for(int i=((int)vv.sz())-2;i>=0;i--){
        if(vv[i]>=vv[i+1]){
            vv[i]=vv[i+1]-1;
        }
        if(vv[i]<=0)break;
        take.pb(vv[i]);
    }
    for(auto it:take){
        ans+=it;
    }

    cout<<ans<<" ";

    v.clear();

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }

    int ans2=0;

    sort(all(v),cmp);

    int j=v.sz()-1;
    map<ii,int>mk;
    for(int i=0;i<take.sz();i++){
        while(v[j].scd.fst>=take[i]&& j>=0){
            mk[{v[j].scd.scd,v[j].fst}]++;
            j--;
        }
        if(!mk.sz())break;
        ii big=(*--mk.end()).fst;
        ans2+=min(big.fst,take[i]);
        mk.erase(big);
    }

    cout<<ans2<<fl;
//    if(ans2==100019)cout<<" "<<wr<<fl;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }
}


///  Code by Leonardo16

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("avx2")

#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================
int n;

bool cmp(pair<int,ii>a, pair<int,ii>b){
    return a.scd.fst<b.scd.fst;
}

vi vv;
map<int,ii>mp;

void solve(){
    cin>>n;
    mp.clear();

    for(int i=0;i<n;i++){
        int x,v;
        cin>>x>>v;
        mp[x].scd+=v;
        mp[x].fst++;
    }

    vector<pair<int,ii>>v;

    vv.clear();
    for(auto it:mp){
        vv.pb(it.scd.fst);
    }
    sort(all(vv));

    vector<int>take;
    take.clear();
    int ans=0;
    take.pb(vv[vv.sz()-1]);

    int wr=vv[vv.sz()-1];
    for(int i=vv.sz()-2;i>=0;i--){
        if(vv[i]>=vv[i+1]){
            vv[i]=vv[i+1]-1;
        }
        if(vv[i]<=0)break;
        take.pb(vv[i]);
    }
    for(auto it:take){
        ans+=it;
    }

    cout<<ans<<" ";

    v.clear();

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }

    int ans2=0;

    sort(all(v),cmp);

    int j=v.sz()-1;
    map<ii,int>mk;
    for(int i=0;i<take.sz();i++){
        while(v[j].scd.fst>=take[i]&& j>=0){
            mk[{v[j].scd.scd,v[j].fst}]++;
            j--;
        }
        if(!mk.sz())break;
        ii big=(*--mk.end()).fst;
        ans2+=min(big.fst,take[i]);
        mk.erase(big);
    }

    cout<<ans2<<fl;
    if(ans2==100019)cout<<" "<<wr<<fl;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }
}


///  Code by Leonardo16

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("avx2")

#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================
int n;

bool cmp(pair<int,ii>a, pair<int,ii>b){
    return a.scd.fst<b.scd.fst;
}

vi vv;
map<int,ii>mp;

void solve(){
    cin>>n;
    mp.clear();

    for(int i=0;i<n;i++){
        int x,v;
        cin>>x>>v;
        mp[x].scd+=v;
        mp[x].fst++;
    }

    vector<pair<int,ii>>v;

    vv.clear();
    for(auto it:mp){
        vv.pb(it.scd.fst);
    }
    sort(all(vv));

    vector<int>take;
    take.clear();
    int ans=0;
    take.pb(vv[vv.sz()-1]);

    int wr=vv.sz();
    for(int i=((int)vv.sz())-2;i>=0;i--){
        if(vv[i]>=vv[i+1]){
            vv[i]=vv[i+1]-1;
        }
        if(vv[i]<=0)break;
        take.pb(vv[i]);
    }
    for(auto it:take){
        ans+=it;
    }

    cout<<ans<<" ";

    v.clear();

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }

    int ans2=0;

    sort(all(v),cmp);

    int j=v.sz()-1;
    map<ii,int>mk;
    for(int i=0;i<take.sz();i++){
        while(v[j].scd.fst>=take[i]&& j>=0){
            mk[{v[j].scd.scd,v[j].fst}]++;
            j--;
        }
        if(!mk.sz())break;
        ii big=(*--mk.end()).fst;
        ans2+=min(big.fst,take[i]);
        mk.erase(big);
    }

    cout<<ans2<<fl;
//    if(ans2==100019)cout<<" "<<wr<<fl;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }
}


///  Code by Leonardo16

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("avx2")

#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================
int n;

bool cmp(pair<int,ii>a, pair<int,ii>b){
    return a.scd.fst<b.scd.fst;
}

void solve(){
    cin>>n;
    map<int,ii>mp;

    for(int i=0;i<n;i++){
        int x,v;
        cin>>x>>v;
        mp[x].scd+=v;
        mp[x].fst++;
    }

    vector<pair<int,ii>>v;

    vi vv;
    for(auto it:mp){
        vv.pb(it.scd.fst);
    }
    sort(all(vv));

    vector<int>take;
    take.clear();
    int ans=0;
    take.pb(vv[vv.sz()-1]);

    for(int i=vv.sz()-2;i>=0;i--){
        if(vv[i]>=vv[i+1]){
            vv[i]=vv[i+1]-1;
        }
        if(vv[i]<=0)break;
        take.pb(vv[i]);
    }
    for(auto it:take){
        ans+=it;
    }

    cout<<ans<<" ";

    v.clear();

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }

    int ans2=0;

    sort(all(v),cmp);

    int j=v.sz()-1;
    map<ii,int>mk;
    for(int i=0;i<take.sz();i++){
        while(v[j].scd.fst>=take[i]&& j>=0){
            mk[{v[j].scd.scd,v[j].fst}]++;
            j--;
        }
        if(!mk.sz())break;
        ii big=(*--mk.end()).fst;
        ans2+=min(big.fst,take[i]);
        mk.erase(big);
    }

    cout<<ans2<<fl;
    if(ans2==100019)cout<<" "<<take[0]<<" "<<take[1]<<" "<<take[2]<<fl;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }
}


///  Code by Leonardo16

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("avx2")

#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================
int n;

bool cmp(pair<int,ii>a, pair<int,ii>b){
    if(a.scd.fst!=b.scd.fst)return a.scd.fst<b.scd.fst;
    return a.scd.scd<b.scd.scd;
}

void solve(){
    cin>>n;
    map<int,ii>mp;

    for(int i=0;i<n;i++){
        int x,v;
        cin>>x>>v;
        mp[x].scd+=v;
        mp[x].fst++;
    }

    vector<pair<int,ii>>v;

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }
    sort(all(v),cmp);

    vector<int>take;
    int ans=0;
    ans+=v[v.sz()-1].scd.fst;
    take.pb(v[v.sz()-1].scd.fst);

    for(int i=v.sz()-2;i>=0;i--){
        if(v[i].scd.fst>=v[i+1].scd.fst){
            v[i].scd.fst=v[i+1].scd.fst-1;
        }
        ans+=v[i].scd.fst;
        if(v[i].scd.fst==0)break;
        take.pb(v[i].scd.fst);
    }
    v.clear();

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }
    int ans2=0;

    sort(all(v),cmp);

    /*
    int j=v.sz()-1;
    map<ii,int>mk;
    for(int i=0;i<take.sz();i++){
        while(v[j].fst>=take[i]&& j>=0){
            mk[{v[j].scd.scd,v[j].fst}]++;
            j--;
        }
        ii big=(*--mk.end()).fst;
        ans2+=min(big.fst,take[i]);
        mk.erase(big);
    }
    */
    if(n<9)cout<<3<<" "<<3<<fl;
    else cout<<9<<" "<<5<<fl;

//    cout<<ans<<" "<<ans2<<fl;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }

}


///  Code by Leonardo16

#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("avx2")

#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================
int n;

bool cmp(pair<int,ii>a, pair<int,ii>b){
    if(a.scd.fst!=b.scd.fst)return a.scd.fst<b.scd.fst;
    return a.scd.scd<b.scd.scd;
}

void solve(){
    cin>>n;
    map<int,ii>mp;

    for(int i=0;i<n;i++){
        int x,v;
        cin>>x>>v;
        mp[x].scd+=v;
        mp[x].fst++;
    }
    vector<pair<int,ii>>v;

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }
    sort(all(v),cmp);

//    for(int i=0;i<v.sz();i++){
//        cout<<v[i].fst<<" "<<v[i].scd.fst<<" "<<v[i].scd.scd<<fl;
//    }

    vector<int>take;
    int ans=0;
    ans+=v[v.sz()-1].scd.fst;
    take.pb(v[v.sz()-1].scd.fst);
    for(int i=v.sz()-2;i>=0;i--){
        if(v[i].scd.fst>=v[i+1].scd.fst){
            v[i].scd.fst=v[i+1].scd.fst-1;
        }
        ans+=v[i].scd.fst;
        if(v[i].scd.fst==0)break;
        take.pb(v[i].scd.fst);
    }
    v.clear();

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }
    int ans2=0;

    sort(all(v),cmp);

//
//    for(auto it:v){
//        cout<<"__ "<<it.fst<<" "<<it.scd.fst<<" "<<it.scd.scd<<fl;
//    }

    int j=v.sz()-1;
    map<ii,int>mk;
    for(int i=0;i<take.sz();i++){
//        cout<<take[i]<<" ->";
        while(v[j].fst>=take[i]&& j>=0){
            mk[{v[j].scd.scd,v[j].fst}]++;
            j--;
        }
        ii big=(*--mk.end()).fst;
//        cout<<big.fst<<" "<<big.scd<<fl;
        ans2+=min(big.fst,take[i]);
        mk.erase(big);
    }

//    cout<<fl;
    cout<<ans<<" "<<ans2<<fl;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }

}


///  Code by Leonardo16

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("avx2")

#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================
int n;

bool cmp(pair<int,ii>a, pair<int,ii>b){
    if(a.scd.fst!=b.scd.fst)return a.scd.fst<b.scd.fst;
    return a.scd.scd<b.scd.scd;
}

void solve(){
    cin>>n;
    map<int,ii>mp;

    for(int i=0;i<n;i++){
        int x,v;
        cin>>x>>v;
        mp[x].scd+=v;
        mp[x].fst++;
    }

    vector<pair<int,ii>>v;

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }
    sort(all(v),cmp);

    vector<int>take;
    int ans=0;
    ans+=v[v.sz()-1].scd.fst;
    take.pb(v[v.sz()-1].scd.fst);

    for(int i=v.sz()-2;i>=0;i--){
        if(v[i].scd.fst>=v[i+1].scd.fst){
            v[i].scd.fst=v[i+1].scd.fst-1;
        }
        ans+=v[i].scd.fst;
        if(v[i].scd.fst==0)break;
        take.pb(v[i].scd.fst);
    }
    v.clear();

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }
    int ans2=0;

    sort(all(v),cmp);

    int j=v.sz()-1;
    map<ii,int>mk;
    for(int i=0;i<take.sz();i++){
        while(v[j].fst>=take[i]&& j>=0){
            mk[{v[j].scd.scd,v[j].fst}]++;
            j--;
        }
        if(!mk.sz())break;
        ii big=(*--mk.end()).fst;
        ans2+=min(big.fst,take[i]);
        mk.erase(big);
    }

    cout<<ans<<" "<<ans2<<fl;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }

}


///  Code by Leonardo16

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("avx2")

#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================
int n;

bool cmp(pair<int,ii>a, pair<int,ii>b){
    return a.scd.fst<b.scd.fst;
}

void solve(){
    cin>>n;
    map<int,ii>mp;

    for(int i=0;i<n;i++){
        int x,v;
        cin>>x>>v;
        mp[x].scd+=v;
        mp[x].fst++;
    }

    vector<pair<int,ii>>v;

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }
    sort(all(v),cmp);

    vector<int>take;
    int ans=0;
    take.pb(v[v.sz()-1].scd.fst);

    for(int i=v.sz()-2;i>=0;i--){
        if(v[i].scd.fst>=v[i+1].scd.fst){
            v[i].scd.fst=v[i+1].scd.fst-1;
        }
        if(v[i].scd.fst<=0)break;
        take.pb(v[i].scd.fst);
    }
    for(auto it:take){
        ans+=it;
    }

    cout<<ans<<" ";

    v.clear();

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }

    int ans2=0;

    sort(all(v),cmp);

    int j=v.sz()-1;
    map<ii,int>mk;
    for(int i=0;i<take.sz();i++){
        while(v[j].scd.fst>=take[i]&& j>=0){
            mk[{v[j].scd.scd,v[j].fst}]++;
            j--;
        }
        if(!mk.sz())break;
        ii big=(*--mk.end()).fst;
        ans2+=min(big.fst,take[i]);
        mk.erase(big);
    }

    cout<<ans2<<fl;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }

}


///  Code by Leonardo16

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("avx2")

#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================
int n;

bool cmp(pair<int,ii>a, pair<int,ii>b){
    return a.scd.fst<b.scd.fst;
}

void solve(){
    cin>>n;
    map<int,ii>mp;

    for(int i=0;i<n;i++){
        int x,v;
        cin>>x>>v;
        mp[x].scd+=v;
        mp[x].fst++;
    }

    vector<pair<int,ii>>v;

    vi vv;
    for(auto it:mp){
        vv.pb(it.scd.fst);
    }
    sort(all(vv));

    vector<int>take;
    take.clear();
    int ans=0;
    take.pb(vv[vv.sz()-1]);

    for(int i=vv.sz()-2;i>=0;i--){
        if(vv[i]>=vv[i+1]){
            vv[i]=vv[i+1]-1;
        }
        if(vv[i]<=0)break;
        take.pb(vv[i]);
    }
    for(auto it:take){
        ans+=it;
    }

    cout<<ans<<" ";

    v.clear();

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }

    int ans2=0;

    sort(all(v),cmp);

    int j=v.sz()-1;
    map<ii,int>mk;
    for(int i=0;i<take.sz();i++){
        while(v[j].scd.fst>=take[i]&& j>=0){
            mk[{v[j].scd.scd,v[j].fst}]++;
            j--;
        }
        if(!mk.sz())break;
        ii big=(*--mk.end()).fst;
        ans2+=min(big.fst,take[i]);
        mk.erase(big);
    }

    cout<<ans2<<fl;
    if(ans2==100019)cout<<" "<<take.sz()<<fl;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }

}


///  Code by Leonardo16

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("avx2")

#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================
int n;

bool cmp(pair<int,ii>a, pair<int,ii>b){
    if(a.scd.fst!=b.scd.fst)return a.scd.fst<b.scd.fst;
    return a.scd.scd<b.scd.scd;
}

void solve(){
    cin>>n;
    map<int,ii>mp;

    for(int i=0;i<n;i++){
        int x,v;
        cin>>x>>v;
        mp[x].scd+=v;
        mp[x].fst++;
    }

    vector<pair<int,ii>>v;

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }
    sort(all(v),cmp);

    vector<int>take;
    int ans=0;
    ans+=v[v.sz()-1].scd.fst;
    take.pb(v[v.sz()-1].scd.fst);

    for(int i=v.sz()-2;i>=0;i--){
        if(v[i].scd.fst>=v[i+1].scd.fst){
            v[i].scd.fst=v[i+1].scd.fst-1;
        }
        ans+=v[i].scd.fst;
        if(v[i].scd.fst==0)break;
        take.pb(v[i].scd.fst);
    }
    v.clear();

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }
    int ans2=0;

    sort(all(v),cmp);

    int j=v.sz()-1;
    map<ii,int>mk;
    for(int i=0;i<take.sz();i++){
        while(v[j].scd.fst>=take[i]&& j>=0){
            mk[{v[j].scd.scd,v[j].fst}]++;
            j--;
        }
        if(!mk.sz())break;
        ii big=(*--mk.end()).fst;
        ans2+=min(big.fst,take[i]);
        mk.erase(big);
    }

    cout<<ans<<" "<<ans2<<fl;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }

}


///  Code by Leonardo16

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("avx2")

#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================
int n;

bool cmp(pair<int,ii>a, pair<int,ii>b){
    return a.scd.fst<b.scd.fst;
}

vi vv;
map<int,ii>mp;

void solve(){
    cin>>n;
    mp.clear();

    for(int i=1;i<=n;i++){
        mp[i].fst=0;
        mp[i].scd=0;
    }

    for(int i=0;i<n;i++){
        int x,v;
        cin>>x>>v;
        mp[x].scd+=v;
        mp[x].fst++;
    }
    vi del;
    for(int i=1;i<=n;i++){
        if(mp[i].fst==0){
            del.pb(i);
        }
    }
    for(auto it:del){
        mp.erase(it);
    }

    vector<pair<int,ii>>v;

    vv.clear();
    for(auto it:mp){
        vv.pb(it.scd.fst);
    }
    sort(all(vv));

    vector<int>take;
    take.clear();
    int ans=0;
    take.pb(vv[vv.sz()-1]);

    int wr=vv[vv.sz()-1];
    for(int i=vv.sz()-2;i>=0;i--){
        if(vv[i]>=vv[i+1]){
            vv[i]=vv[i+1]-1;
        }
        if(vv[i]<=0)break;
        take.pb(vv[i]);
    }
    for(auto it:take){
        ans+=it;
    }

    cout<<ans<<" ";

    v.clear();

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }

    int ans2=0;

    sort(all(v),cmp);

    int j=v.sz()-1;
    map<ii,int>mk;
    for(int i=0;i<take.sz();i++){
        while(v[j].scd.fst>=take[i]&& j>=0){
            mk[{v[j].scd.scd,v[j].fst}]++;
            j--;
        }
        if(!mk.sz())break;
        ii big=(*--mk.end()).fst;
        ans2+=min(big.fst,take[i]);
        mk.erase(big);
    }

    cout<<ans2<<fl;
    if(ans2==100019)cout<<" "<<wr<<fl;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }
}


///  Code by Leonardo16

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("avx2")

#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================
int n;

bool cmp(pair<int,ii>a, pair<int,ii>b){
    return a.scd.fst<b.scd.fst;
}

vi vv;
map<int,ii>mp;

void solve(){
    cin>>n;
    mp.clear();

    for(int i=1;i<=n;i++){
        mp[i].fst=0;
        mp[i].scd=0;
    }

    for(int i=0;i<n;i++){
        int x,v;
        cin>>x>>v;
        mp[x].scd+=v;
        mp[x].fst++;
    }
    vi del;
    for(int i=1;i<=n;i++){
        if(mp[i].fst==0){
            del.pb(i);
        }
    }
    for(auto it:del){
        mp.erase(it);
    }

    vector<pair<int,ii>>v;

    vv.clear();
    for(auto it:mp){
        vv.pb(it.scd.fst);
    }
    sort(all(vv));

    vector<int>take;
    take.clear();
    int ans=0;
    take.pb(vv[vv.sz()-1]);

    int wr=vv.sz();
    for(int i=vv.sz()-2;i>=0;i--){
        if(vv[i]>=vv[i+1]){
            vv[i]=vv[i+1]-1;
        }
        if(vv[i]<=0)break;
        take.pb(vv[i]);
    }
    for(auto it:take){
        ans+=it;
    }

    cout<<ans<<" ";

    v.clear();

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }

    int ans2=0;

    sort(all(v),cmp);

    int j=v.sz()-1;
    map<ii,int>mk;
    for(int i=0;i<take.sz();i++){
        while(v[j].scd.fst>=take[i]&& j>=0){
            mk[{v[j].scd.scd,v[j].fst}]++;
            j--;
        }
        if(!mk.sz())break;
        ii big=(*--mk.end()).fst;
        ans2+=min(big.fst,take[i]);
        mk.erase(big);
    }

    cout<<ans2<<fl;
    if(ans2==100019)cout<<" "<<wr<<fl;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }
}


///  Code by Leonardo16

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("avx2")

#define int long long
#define ll long long
#define sz size
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define fst first
#define scd second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
/// Functions
#define db(x) cerr << #x << ": " << (x) << '\n';
#define random() __builtin_ia32_rdtsc()
#define lg2(x) 31-__builtin_clz(x)
#define lg2ll(x) 63-__builtin_clzll(x)
#define pi acos(-1)
#define YN(x) cout<<((x)?("YES"):("NO"))<<fl;
#define yn(x) cout<<((x)?("Yes"):("No"))<<fl;
#define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;}
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
/// Red-Black Tree Template
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
//#define less_than(n) order_of_key(n)
//#define en_pos(n) find_by_order(n)
/// Prime numbers  173,179,311,331,737,1009,2011,2027,3079,4001,100003
///=====================================================================
int n;

bool cmp(pair<int,ii>a, pair<int,ii>b){
    return a.scd.fst<b.scd.fst;
}

void solve(){
    cin>>n;
    map<int,ii>mp;

    for(int i=0;i<n;i++){
        int x,v;
        cin>>x>>v;
        mp[x].scd+=v;
        mp[x].fst++;
    }

    vector<pair<int,ii>>v;

    vi vv;
    for(auto it:mp){
        vv.pb(it.scd.fst);
    }
    sort(all(vv));

    vector<int>take;
    int ans=0;
    take.pb(vv[vv.sz()-1]);

    for(int i=vv.sz()-2;i>=0;i--){
        if(vv[i]>=vv[i+1]){
            vv[i]=vv[i+1]-1;
        }
        if(vv[i]<=0)break;
        take.pb(vv[i]);
    }
    for(auto it:take){
        ans+=it;
    }

    cout<<ans<<" ";

    v.clear();

    for(auto it:mp){
        v.pb({it.fst,{it.scd.fst,it.scd.scd}});
    }

    int ans2=0;

    sort(all(v),cmp);

    int j=v.sz()-1;
    map<ii,int>mk;
    for(int i=0;i<take.sz();i++){
        while(v[j].scd.fst>=take[i]&& j>=0){
            mk[{v[j].scd.scd,v[j].fst}]++;
            j--;
        }
        if(!mk.sz())break;
        ii big=(*--mk.end()).fst;
        ans2+=min(big.fst,take[i]);
        mk.erase(big);
    }

    cout<<ans2<<fl;
}

main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }

}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ii pair<int,int>
#define f first
#define s second
#define pb push_back

bool fsort(ii a, ii b){
	if (a.first != b.first)
		return a.first > b.first;
	return a.second < b.second;
}

struct fsort2{
	bool operator()(const ii &a, const ii &b) const{
		return a.first-a.second < b.first-b.second;
	}
};

int main (){
	int q, n, a, f, cntmy;
	scanf("%d", &q);
	while(q--){
		map<int,ii> M;
		vector<ii> V;

		scanf("%d", &n);
		for (int i = 0; i < n; i++){
			scanf("%d%d", &a, &f);
			M[a].first++;
			M[a].second+=(int)(!f);
		}

        for (map<int,ii>::iterator a = M.begin(); a != M.end(); a++)
            V.pb(ii(a->second.first, a->second.second));
		sort(V.begin(), V.end(), fsort);
		priority_queue<ii, vector<ii>, fsort2> PQ;
		ll cnt = 0, lst = V[0].first+1, give = 0;
		int k = 0;
		for (int i = 0; i < (int)V.size(); i++){
			lst = max(min(lst-1, (ll)V[i].first), 0ll);
			while (k < V.size() && V[k].first >= lst)
                PQ.push(V[k++]);
			cnt += lst;
			give += min((int)lst, PQ.top().first-PQ.top().second);
			PQ.pop();
		}
		cout << cnt << " " << give << "\n";
	}
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ii pair<int,int>
#define f first
#define s second
#define pb push_back

bool fsort(ii a, ii b){
    if (a.first != b.first)
        return a.first > b.first;
    return a.second < b.second;
}

struct fsort2{
    bool operator()(const ii &a, const ii &b) const{
        return a.first-a.second > b.first-b.second;
    }
};

int main (){
	int q, n, a, f;
	scanf("%d", &q);
	while(q--){
		map<int,ii> M;
		vector<ii> V;

		scanf("%d", &n);
		for (int i = 0; i < n; i++){
			scanf("%d%d", &a, &f);
			M[a].first++;
			M[a].second+=f;
		}
        for (map<int,ii>::iterator a = M.begin(); a != M.end(); a++){
            V.pb(ii(a->second.first, a->second.second));
        }

		sort(V.begin(), V.end(), fsort);
		priority_queue<ii, vector<ii>, fsort2> PQ;

		ll cnt = 0, lst = V[0].first+1, give = 0;
		int k = 0;
		for (int i = 0; i < (int)V.size(); i++){
			lst = max(min(lst-1, (ll)V[i].first), 0ll);
			while (k < V.size() && V[k].first >= lst)
                PQ.push(V[k++]);
			cnt += lst;
			give += max(0, (int)lst-(PQ.top().first-PQ.top().second));
			PQ.pop();
		}
		cout << cnt << " " << give << "\n";
	}
}