This problem is actually a subproblem of problem G from the same contest.

There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$).

You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). 

It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.

Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.

You have to answer $q$ independent queries.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.


-----Input-----

The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Each query is represented by two lines.

The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of candies.

The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box.

It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$.


-----Output-----

For each query print one integer — the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.


-----Example-----
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7

Output
3
10
9



-----Note-----

In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies.

Note that this is not the only possible solution — taking two candies of type $4$ and one candy of type $6$ is also valid.

Difficulty: introductory

Starter code:

Solutions:
import sys
input = sys.stdin.readline

Q = int(input())
for _ in range(Q):
    N = int(input())
    A = [int(a) for a in input().split()]
    X = {}
    for a in A:
        if a in X:
            X[a] += 1
        else:
            X[a] = 1
    Y = []
    for x in X:
        Y.append(X[x])
    Y = sorted(Y)[::-1]
    prev = Y[0] + 1
    su = 0
    for i in range(len(Y)):
        ne = min(prev-1, Y[i])
        if ne <= 0:
            break
        su += ne
        prev = ne
    print(su)



import sys
from collections import Counter
import itertools

def input():
    return sys.stdin.readline().strip()

def dinput():
    return int(input())

def tinput():
    return input().split()

def rinput():
    return map(int, tinput())

def main():
    input()
    q = {}
    for i in tinput():
        if i not in q:
            q[i] = 1
        else:
            q[i] = q[i] + 1
    r = []
    for i in q:
        r.append(q[i])
    r.sort()
    y = 0
    t = []
    for i in range(len(r)):
        while r[i] in t and r[i] > 0:
            r[i] -= 1
        t.append(r[i])
        y += t[i]
    print(y)
        
for i in range(int(input())):
    main()

q = int(input())
for _ in range(q):
    n = int(input())
    a = list(map(int, input().split()))
    u = [0] * n
    for i in range(n):
        u[a[i] - 1] += 1
    u.sort(reverse = 1)
    k = u[0]
    ans = k
    for i in range(1, n):
        if u[i] >= u[i - 1]:
            u[i] = u[i - 1] - 1
            if u[i] <= 0:
                break
        ans += u[i]
    print(ans)
    #print(u)


import sys
input = sys.stdin.readline
from collections import Counter

Q=int(input())
for testcase in range(Q):
    n=int(input())
    A=Counter((list(map(int,input().split()))))
    S=sorted(list(A.values()),reverse=True)

    #print(A)
    #print(S)
    NOW=10**10

    ANS=0
    for s in S:
        M=min(NOW,s)
        ANS+=M
        NOW=M-1

        if NOW==0:
            break

    print(ANS)
        

    


SS = []
a = int(input())
for i in range(a):
    x = int(input())
    A = list(map(int, input().split()))
    D = {}
    for j in range(x):
        if A[j] in D:
            D[A[j]] += 1
        else:
            D[A[j]] = 1
    B = []
    for key in D:
        B.append(D[key])
    B.sort(reverse=True)
    w = B[0]
    for j in range(1, len(B)):
        if B[j] < B[j - 1] - 1:
            w += max(0, B[j])
        else:
            B[j] = B[j - 1] - 1
            w += max(0, B[j - 1] - 1)
    SS.append(w)
for s in SS:
    print(s)

for i in range(int(input())):
    n = int(input())
    sort_col = [0] * (n + 1)
    col_csort = [0] * (n + 1)
    t = 0
    for el in map(int, input().split()):
        col_csort[sort_col[el]] -= 1
        sort_col[el] += 1
        col_csort[sort_col[el]] += 1
        t = max(sort_col[el], t)
    cou = 0
    for j in range(t, 0, -1):
        if col_csort[j]:
            cou += j
            col_csort[j] -= 1
            if col_csort[j]:
                col_csort[j - 1] += col_csort[j]
    print(cou)


T = int(input())
test = 0
while test<T:
    
    N = int(input())
    
    arr = input()
    arr = [int(num) for num in arr.split(' ')]
    
    freq = {}
    
    for num in arr:
        if num not in freq:
            freq[num] = 1
        else:
            freq[num] += 1
          
    res = 0
    
    k = list(freq.values())
    k.sort(reverse=True)
    #print(k)
    m = max(k)
    
    idx = 0
    while m>0 and idx<len(k):
        m = min(m,k[idx])
        res += m
        m -= 1
        idx += 1
        
    print(res)
        
    test += 1

for q in range(int(input())):
    n = int(input())
    A = list(map(int, input().split()))
    G = {}
    for i in A:
        if i not in G:
            G[i] = 0
        G[i] += 1
    B = []
    for i in G:
        B.append(G[i])
    B.sort(reverse=True)
    cnt = 0
    op = B[0] + 1
    for i in B:
        if i >= op:
            op -= 1
            cnt += max(0, op)
        else:
            op = i
            cnt += i
        if op == 0:
            break
    print(cnt)

import sys
t=int(input())
for _ in range(t):
    n=int(sys.stdin.readline())
    a=list(map(int,sys.stdin.readline().split()))
    d={}
    for i in a:
        d[i]=d.get(i,0) + 1
    b=sorted(list(((list(d.values())))))
    ans=b[-1]
    ans1=b[-1]
    # print(b)
    # print(ans,ans1)
    for i in range(len(b)-2,-1,-1):
        # print(" ",b[i])
        # print("ans",ans,ans1,b[i])
        ans1=min(ans1-1,b[i])
        if ans1>0:
            ans+=ans1
        else:
            break
    print(ans)        
            
    


#   In this template you are not required to write code in main

import sys
inf = float("inf")

#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
#from math import ceil,floor,log,sqrt,factorial,pow,pi,gcd
#from bisect import bisect_left,bisect_right

abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod,MOD=1000000007,998244353
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()

q=int(input())
while q>0:
    n=int(input())
    Arr=get_array()
    mydict=dict()
    for i in Arr:
        mydict[i]=mydict.get(i,0)+1
    myset=set()
    count=0
    for i in mydict:
        if mydict[i] not in myset:
            count+=mydict[i]
            myset.add(mydict[i])
        else:
            z=mydict[i]
            for i in range(z,0,-1):
                if i not in myset:
                    count+=i
                    myset.add(i)
                    break
    print(count)
    q-=1

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time

q   = int(input())
ans = []

start = time.time()

for i in range(q):
    n   = int(input())
    a   = [int(j) for j in input().split()]
    d   = [ 0 for j in range(n)]
    for j in range(n):
        d[a[j]-1]+=1
    d.sort(reverse=True)
    s = d[0]
    last = d[0]
    for j in range(1, n):
        if last > d[j]:
            s += d[j]
            last = d[j]
        else:
            last -= 1
            if last <= 0:
                break
            s += last
    ans.append(s)

for i in range(q):
    print(ans[i])
finish = time.time()
#print(finish - start)


q=int(input())
for i in range(q):
    n=int(input())
    a=[int(x) for x in input().split()]
    dic={}
    b=[]
    counter=0
    for item in a:
        if item not in dic:
            dic[item]=1
        else:
            dic[item]+=1
    for item in dic:
        b.append(dic[item])
    b.sort(reverse=True)
    x=b[0]+1
    for item in b:
        x=min(x-1,item)
        counter+=max(x,0)
    print(counter)
        
            


import sys
input=sys.stdin.readline
def ecount(X):
  c,ans=1,[]
  for i in range(len(X)-1):
    if X[i]==X[i+1]:c+=1
    else:
      ans.append(c);c=1
  ans.append(c)
  return ans
for i in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    a.sort()
    b=ecount(a)
    b.sort(reverse=1)
    ans=b[0]
    m=b[0]
    for i in b[1:]:
        m=min(max(0,m-1),i)
        ans+=m
    print(ans)

t = int(input())

while t > 0:
    n = int(input())
    a = list(map(int,input().split()))

    P = (1 + n) * [0]
    for i in a:
        P[i] += 1

    b = []
    for i in range(1 + n):
        if P[i] != 0:
            b.append(P[i])

    b.sort()

    m = len(b) - 1
    ans = 0
    take = b[m]
    
    for i in range(m, -1, -1):
        if take < 0:
            break
        if 0 < b[i] <= take:
            ans += b[i]
            take = b[i] - 1
        elif b[i] > take:
            ans += take
            take -= 1

    print(ans)
    
    t -= 1

#   In this template you are not required to write code in main

import sys
inf = float("inf")

#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
#from math import ceil,floor,log,sqrt,factorial,pow,pi,gcd
#from bisect import bisect_left,bisect_right

abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod,MOD=1000000007,998244353
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()

Q=int(input())
while Q>0:
    mydict1 = dict()
    n=int(input())
    a=get_array()
    for j in a:
        mydict1[j]=mydict1.get(j,0)+1
    ct = 0
    myset1=set()
    for i in mydict1:
        if mydict1[i] not in myset1:
            ct+=mydict1[i]
            myset1.add(mydict1[i])
        else:
            z=mydict1[i]
            for i in range(z,0,-1):
                if i not in myset1:
                    ct+=i
                    myset1.add(i)
                    break
    print(ct)
    Q-=1

T=int(input())
for i in range(T):
    n=int(input())
    s=[list(map(int,input().split()))]
    p={}
    q={}
    for it in s:
        for j in range(n):
            if it[j] in q:
                q[it[j]]+=1
            else:
                q[it[j]]=1
    for j in range(1,n+1):
        if j in q:
            if q[j] in p:
                p[q[j]]+=1
            else:
                p[q[j]]=1
    x=n
    ans=0
    cnt=0
    while x>0:
        if x in p:
            cnt+=p[x]
        if cnt>0:
            ans+=x
            cnt-=1
        x-=1
    print(ans)
    p.clear()
    q.clear()
    s.clear()


q = int(input())
for j in range(q):
    n = int(input())
    a = list(map(int,input().split()))
    b = [0]*n
    for i in a:
        b[i-1] += 1
    b.sort()
    k = b[n-1]+1
    ans = 0
    for i in range(n-1,-1,-1):
        if b[i] >= k:
            b[i] = k-1
        k = b[i]
        ans += max(0,b[i]) 
    print(ans)

from collections import *
from sys import stdin

def input():
    return stdin.readline()

q = int(input())
for _ in range(q):
    n = int(input())
    aas = list(map(int, input().split()))
    counts = [0]*(n+1)
    for a in aas:
        counts[a] += 1
    counts.sort(reverse=True)
    total = 0
    mx = counts[0]
    for c in counts:
        inc = min(mx, c)
        if inc == 0:
            break
        mx = inc - 1
        total += inc
    print(total)


from collections import Counter
t=int(input())
ANS = []
for _ in range(t):
    n=int(input()) 
    a=list(map(int,input().split()))
    freq = sorted(list(dict(Counter(a)).values()),reverse = True)
    i = 1
    ans = freq[0]
    last = freq[0]
    while i < len(freq) :
        val = freq[i]
        if val >= last:
            val = last -1 
        if(val<=0):
            break
        else:
            ans+=val
            i+=1
            last = val
    ANS.append(ans)
for i in ANS:
    print(i) 


q = int(input())

for _ in range(q):

	n = int(input())
	a = sorted(list(map(int, input().split())))

	s = []
	last = a[0]
	count = 1
	for i in range(1, n):
		if a[i] != last:
			s.append(count)
			last = a[i]
			count = 0
		count += 1
	s.append(count)

	s.sort()

	answer = s[-1]

	for i in range(len(s) - 2, -1, -1):
		if s[i] >= s[i + 1]:
			s[i] = s[i + 1] - 1
		if not s[i]:
			break
		answer += s[i]

	print(answer)


'''input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
'''
from sys import stdin, setrecursionlimit
from itertools import combinations
import collections

def doc(s):
    if hasattr(s, '__call__'):
        s = s.__doc__
    def f(g):
        g.__doc__ = s
        return g
    return f

class heapdict(collections.MutableMapping):
    __marker = object()

    @staticmethod
    def _parent(i):
        return ((i - 1) >> 1)

    @staticmethod
    def _left(i):
        return ((i << 1) + 1)

    @staticmethod
    def _right(i):
        return ((i+1) << 1)    
    
    def __init__(self, *args, **kw):
        self.heap = []
        self.d = {}
        self.update(*args, **kw)

    @doc(dict.clear)
    def clear(self):
        self.heap.clear()
        self.d.clear()

    @doc(dict.__setitem__)
    def __setitem__(self, key, value):
        if key in self.d:
            self.pop(key)
        wrapper = [value, key, len(self)]
        self.d[key] = wrapper
        self.heap.append(wrapper)
        self._decrease_key(len(self.heap)-1)

    def _min_heapify(self, i):
        l = self._left(i)
        r = self._right(i)
        n = len(self.heap)
        if l < n and self.heap[l][0] < self.heap[i][0]:
            low = l
        else:
            low = i
        if r < n and self.heap[r][0] < self.heap[low][0]:
            low = r

        if low != i:
            self._swap(i, low)
            self._min_heapify(low)

    def _decrease_key(self, i):
        while i:
            parent = self._parent(i)
            if self.heap[parent][0] < self.heap[i][0]: break
            self._swap(i, parent)
            i = parent

    def _swap(self, i, j):
        self.heap[i], self.heap[j] = self.heap[j], self.heap[i]
        self.heap[i][2] = i
        self.heap[j][2] = j

    @doc(dict.__delitem__)
    def __delitem__(self, key):
        wrapper = self.d[key]
        while wrapper[2]:
            parentpos = self._parent(wrapper[2])
            parent = self.heap[parentpos]
            self._swap(wrapper[2], parent[2])
        self.popitem()

    @doc(dict.__getitem__)
    def __getitem__(self, key):
        return self.d[key][0]

    @doc(dict.__iter__)
    def __iter__(self):
        return iter(self.d)

    def popitem(self):
        """D.popitem() -> (k, v), remove and return the (key, value) pair with lowest\nvalue; but raise KeyError if D is empty."""
        wrapper = self.heap[0]
        if len(self.heap) == 1:
            self.heap.pop()
        else:
            self.heap[0] = self.heap.pop(-1)
            self.heap[0][2] = 0
            self._min_heapify(0)
        del self.d[wrapper[1]]
        return wrapper[1], wrapper[0]    

    @doc(dict.__len__)
    def __len__(self):
        return len(self.d)

    def peekitem(self):
        """D.peekitem() -> (k, v), return the (key, value) pair with lowest value;\n but raise KeyError if D is empty."""
        return (self.heap[0][1], self.heap[0][0])

del doc
__all__ = ['heapdict']


def check(num, arr):
	print(num)
	for i in range(len(arr)):
		if abs(arr[i] - num) <= k:
			pass
		else:
			return False
	return True


def get_heap(arr, n):
	mydict = dict()
	for i in arr:
		if i in mydict:
			mydict[i] +=1 
		else:
			mydict[i] = 1

	mheap = heapdict()
	for i in mydict:
		mheap[i] = -mydict[i]
	return mheap


# main starts
q = int(stdin.readline().strip())
for _ in range(q):
	n = int(stdin.readline().strip())
	arr = list(map(int, stdin.readline().split()))
	mheap = get_heap(arr, n)
	t, value = mheap.popitem()
	num = -value
	ans = num	
	while len(mheap) > 0 and num > 0:
		num -= 1
		t, value = mheap.popitem()
		value = -value
		if value >= num:
			ans += num
		else:
			num = value
			ans += num
	print(ans)




import sys
for _ in range(int(input())):
    n=int(sys.stdin.readline())
    a=list(map(int,sys.stdin.readline().split()))
    d={}
    for i in range(n):
        if a[i] in d:
            d[a[i]]+=1
        else:
            d[a[i]]=1
    #print(d)
    b=list(dict.values(d))
    c=[0]*n
    ans=0
    if b[0]==n:
        print(n)
    else:
        for i in range(len(b)-1,-1,-1):
            if c[b[i]]==0:
                ans+=b[i]
                c[b[i]]=1
                #print("adfasf")
            else:
                while c[b[i]]==1:
                    b[i]-=1
                c[b[i]]=1
                c[0]=0
                ans+=b[i]
            #print(b[i])
        print(ans)