import java.io.*;
import java.util.*;

public class codeforces {
	public static PrintWriter out;
	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;
		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}
	// The next two void functions are shuffle functions on an array.
	// This way, quick sort encounters the worst case scenario with low probability.
	// A reason for doing this is if we don't a primitive data array to an object array.
	// For safety reasons, it would be preferable to use object arrays unless doing so expends a lot of time.
	public static void shuffleLongArray(long[] arr) {
		int n = arr.length;
		Random rnd = new Random();
        for (int i=0; i < n; i++) {
        	long tmp = arr[i];
        	int randomPos = i + rnd.nextInt(n-i);
        	arr[i] = arr[randomPos];
        	arr[randomPos] = tmp;
        }
	}
	public static void shuffleIntArray(int[] arr) {
		int n = arr.length;
		Random rnd = new Random();
        for (int i=0; i < n; i++) {
        	int tmp = arr[i];
        	int randomPos = i + rnd.nextInt(n-i);
        	arr[i] = arr[randomPos];
        	arr[randomPos] = tmp;
        }
	}
	public static void main (String[] args) throws IOException {
    	MyScanner sc = new MyScanner();
    	out = new PrintWriter(new BufferedOutputStream(System.out));
    	// Write your solution here.
    	int t = sc.nextInt();
    	for (int asap=0; asap < t; asap++) {
    		int n = sc.nextInt();
    		long[][] paypal = new long[n][2]; // first variable is the people condition; second variable is the coins condition
    		for (int i=0; i < n; i++) {
    			paypal[i][0] = sc.nextLong(); paypal[i][1] = sc.nextLong();
    		}
    		Random rnd = new Random();
            for (int i=0; i < n; i++) {
            	long tmp = paypal[i][0]; long tmp2 = paypal[i][1];
            	int randomPos = i + rnd.nextInt(n-i);
            	paypal[i][0] = paypal[randomPos][0];
            	paypal[i][1] = paypal[randomPos][1];
            	paypal[randomPos][0] = tmp;
            	paypal[randomPos][1] = tmp2;
            }
    		Arrays.sort(paypal, new Comparator<long[]>() {
    			public int compare (long[] a, long[] b) {
    				if (Long.compare(a[0], b[0]) != 0) return Long.compare(a[0], b[0]);
    				return Long.compare(a[1], b[1]);
    			}
    		});
    		// I am going to try to do this problem recursively.
    		int votes = 0; // we want n votes
    		int[] bought = new int[n];
    		int h = 0;
    		long money = 0L;
    		while (h < n) {
    			int i = h;
    			if (votes >= n) break;
    			// What do we need to secure all the votes up to i?
    			if (bought[i] == 1) {
    				h++;
    				continue;
    			}
    			if (votes >= paypal[i][0]) {
    				votes++;
    				bought[i] = 1;
    				h++;
    				continue; // since we secured it
    			}
    			else {
    				// This is the interesting part.
    				// I find the cheapest vote to buy here, and I buy it.
        			long value = 1000000001L;
        			int com = 0;
        			for (int j=i; j < n; j++) {
        				if (paypal[i][0]-votes==1 && j==i) continue;
        				if (paypal[j][1] < value && bought[j] == 0) {
        					value = paypal[j][1];
        					com = j;
        				}
        			}
        			bought[com] = 1;
        			votes++;
        			money += paypal[com][1];
    			}
    		}
    		out.println(money);
    	}
    	out.close();
	}
}