'''
Created on 2019. 9. 21.

@author: kkhh88
'''
#q = int(input())
#x, y = map(int,input().split(' '))

q = int(input())
for _ in range(q):    
    n = int(input())
    lr = []
    for i in range(n):
        lr.append(list(map(int,input().split(' '))))
    
    lr.sort(key=lambda x:x[1], reverse = True)
    lr.sort(key=lambda x:x[0])
    
    cnt = 0
    for i in range(n):
        if lr[i][0] > i or lr[i][0] == 0:
            lr[i][1] = 1000000000
            if lr[i][0] - i > cnt:
                cnt = lr[i][0] - i    
    
    lr.sort(key=lambda x:x[1])
    ans = 0
    for i in range(cnt):
        ans = ans + lr[i][1]
    print (cnt, lr)
    print (ans)