#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include <cassert>
using namespace std;
int n;

struct Pair
{
	int day, val;

	Pair(int _day, int _val): day(_day), val(_val)
	{
	}


	bool operator<(const Pair &a)
	{
		if(day < a.day) return true;
		if(day > a.day) return false;
		return val < a.val;
	}
};

vector<Pair> arr;

int solve()
{
	sort(arr.begin(), arr.end());
	priority_queue<int, vector<int>, less<int> > que;
	int i = arr.size() - 1, day, curDay = arr[i].day, res = 0;
	while(curDay > 0)
	{
		if(i >= 0 && curDay <= arr[i].day)
		{
			que.push(arr[i].val);
			i--;
			while(i >= 0 && arr[i].day == arr[i + 1].day)
			{
				que.push(arr[i].val);
				i--;
			}
		}
		if(!que.empty())
		{
			res += que.top();
			que.pop();
		}
		curDay--;
	}
	return res;
}

int main()
{
	freopen("graphic.in", "r", stdin);
	//freopen("graphic.out", "w", stdout);
	int test, i, a, b;
	cin >> test;
	assert(test > 0);
	while(test > 0)
	{
		cin >> n;
		assert(n >= 1 && n <= 1000);
		for(i = 0; i < n; i++)
		{
			cin >> a >> b;
			assert(a >= 1 && a <= 100000);
			assert(b >= 1 && b <= 100000);
			arr.push_back(Pair(a, b));
		}
		
		cout << solve() << endl;
		arr.clear();
		test--;
	}
	return 0;
}



