t = int(input())

for k in range(t):
    n = int(input())
    arr = []
    for i in range(n):
        arr.append([int(c) for c in input().split(' ')])
    
    arr = sorted(arr)
    cost = 0
    v = 0
    while len(arr)!=0:
        while(len(arr)>0 and arr[0][0]<=v):
            arr.pop(0) 
            v+=1
        if len(arr)!=0:
            if v+1==arr[0][0]:
                _min=len(arr)-1
                for i in range(len(arr)):
                    if arr[i][0]==v+1:
                        continue
                    if arr[_min][1] > arr[i][1]:
                        _min = i
                cost+=arr[_min][1]
                arr.pop(i)
                v+=1
            else:
                _min=0
                for i in range(len(arr)):
                    if arr[_min][1] > arr[i][1]:
                        _min = i
                cost+=arr[_min][1]
                arr.pop(i)
                v+=1
    print(cost)