import java.util.*;
import java.io.*;
public class E {

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int T = Integer.parseInt(st.nextToken());
		for(int t = 0; t < T; t++) {
			st = new StringTokenizer(br.readLine());
			int n = Integer.parseInt(st.nextToken());
			ArrayList<Long>[] costs = new ArrayList[n];
			for(int i = 0; i < n; i++) {
				costs[i] = new ArrayList<>();
			}
			for(int i = 0; i < n; i++) {
				st = new StringTokenizer(br.readLine());
				int m = Integer.parseInt(st.nextToken());
				long p = Integer.parseInt(st.nextToken());
				costs[m].add(p);
			}
			int[] acc = new int[n];
			acc[0] = costs[0].size();
			for(int i = 1; i < n; i++) {
				acc[i] = acc[i-1] + costs[i].size();
			}
			long res = 0;
			PriorityQueue<Long> q = new PriorityQueue<>();
			int d = 0;
			for(int i = n-1; i >= 1; i--) {
				q.addAll(costs[i]);
				while(acc[i-1] + d < i) {
					res += q.poll();
					d++;
				}
			}
			System.out.println(res);
		}
	}

}
