
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.io.*;
public class Main {
	public static void main(String args[]) throws IOException
	{
		StreamTokenizer sc=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		sc.nextToken();int t=(int) sc.nval;
		while(t--!=0)
		{
			sc.nextToken();int n=(int) sc.nval;
			long ans=0;
			PriorityQueue<Integer> q=new PriorityQueue<Integer>();
			ArrayList<Integer> g[]=new ArrayList[n+1];
			for(int i=0;i<=n;i++)
			{
				//q[i]=new PriorityQueue<Integer>();
				g[i]=new ArrayList<Integer>();
			}
			for(int i=1;i<=n;i++)
			{
				sc.nextToken();int m=(int) sc.nval;sc.nextToken();int p=(int) sc.nval;
				g[m].add(p);
			}
			for(int i=n-1;i>=0;i--)
			{
				for(int j=0;j<g[i].size();j++)
					q.add(g[i].get(j));
				while(q.size()>n-i) ans+=q.poll();
			}
			System.out.println(ans);
			
		}
	}
	
}
