// Coached by rainboy
import java.io.*;
import java.util.*;

public class CF1251E2 extends PrintWriter {
	CF1251E2() { super(System.out); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		CF1251E2 o = new CF1251E2(); o.main(); o.flush();
	}

	int[] pq, iq; int cnt;
	int[] pp;
	boolean less(int u, int v) { return pp[u] > pp[v]; }
	int i2(int i) {
		return (i *= 2) > cnt ? 0 : i < cnt && less(pq[i + 1], pq[i]) ? i + 1 : i;
	}
	void pq_up(int u) {
		int i, j, v;
		for (i = iq[u]; (j = i / 2) > 0 && less(u, v = pq[j]); i = j)
			pq[iq[v] = i] = v;
		pq[iq[u] = i] = u;
	}
	void pq_dn(int u) {
		int i, j, v;
		for (i = iq[u]; (j = i2(i)) > 0 && less(v = pq[j], u); i = j)
			pq[iq[v] = i] = v;
		pq[iq[u] = i] = u;
	}
	void pq_add(int u) {
		iq[u] = ++cnt; pq_up(u);
	}
	int pq_remove_first() {
		int u = pq[1], v = pq[cnt--];
		if (v != u) {
			iq[v] = 1; pq_dn(v);
		}
		return u;
	}
	void main() {
		int t = sc.nextInt();
		while (t-- > 0) {
			int n = sc.nextInt();
			int[] mm = new int[n];
			pp = new int[n];
			Integer[] ii = new Integer[n];
			long sum = 0;
			for (int i = 0; i < n; i++) {
				mm[i] = sc.nextInt();
				pp[i] = sc.nextInt();
				ii[i] = i;
				sum += pp[i];
			}
			Arrays.sort(ii, (i, j) -> mm[i] - mm[j]);
			pq = new int[1 + n]; iq = new int[n]; cnt = 0;
			for (int m = 0, i = 0; m < n; m++) {
				while (i < n && mm[ii[i]] <= m)
					pq_add(ii[i++]);
				if (cnt > 0) {
					int i_ = pq_remove_first();
					sum -= pp[i_];
				}
			}
			println(sum);
		}
	}
}
