#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

#define maxsrok 100010

struct order
{
	int srok; 
	int money;	
	int index;
};

int compare( const void *arg1, const void *arg2 )
{   
	if (((order*)arg1)->money != ((order*)arg2)->money) return ((order*)arg2)->money - ((order*)arg1)->money;
	else return ((order*)arg2)->srok - ((order*)arg1)->srok;
}

int main()
{
	freopen("graphic.in", "r", stdin);
	//freopen("graphic.out", "w", stdout);

	int test;
	cin >> test;
	for(int t=0; t<test; t++)
	{
		order a[1000];
		int used[maxsrok]={0};
		int n;
		int res = scanf("%d", &n);
		for(int i=0; i<n; i++)
		{
			scanf("%d%d", &a[i].srok, &a[i].money);						
			a[i].index = i;
		}
		qsort(a, n, sizeof(order), compare);

		for(int i=0; i<n; i++)
		{
			for(int j=a[i].srok; j>0; j--)
				if (used[j]==0)
				{
					//used[j]=a[i].index;
					used[j]=a[i].money;
					break;
				}
		}
		int kol=0;
		int sum = 0;
		for(int i=0; i<maxsrok; i++)
			if (used[i]) kol++;		
		for(int i=0; i<maxsrok; i++)
			if (used[i]) 
				{
					sum+=used[i];
					//printf("%d ", used[i]);
				}

		cout << sum << endl;
	}
	return 0;
}

