

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	/*1
	3
	1 5
	2 10
	2 8*/
	public static void main(String args[]) {
		InputReader sc=new InputReader(System.in);
		int maxn=5010;
		ArrayList<Integer>[] v=new ArrayList[maxn];
		for(int i=0;i<maxn;i++) {
			v[i]=new ArrayList<Integer>();
		}
		int t=sc.nextInt();
		PriorityQueue<Integer> q=new PriorityQueue<>();
		//初始化不要忘记
		//检查数据类型不要忘记
		while(t-->0) {
			int n=sc.nextInt();
			for(int i=0;i<n;i++) {
				v[i].clear();
			}
			for(int i=0;i<n;i++) {
				int a=sc.nextInt();
				int b=sc.nextInt();
				v[a].add(b);
			}
			q.clear();
			int cnt=0;
			int prex=n;
			long sum=0;
			for(int i=n-1;i>=0;i--) {
				prex-=v[i].size();
				int need=i-prex;
				//System.out.println(need);
				for(int j=0;j<v[i].size();j++) {
					q.add(v[i].get(j));
					//System.out.println(v[i].get(j));
				}
				//System.out.println(q.peek());
				while(cnt < need){
					++cnt;
					sum += q.poll();				
				}			
			}
			System.out.println(sum);
		}
		
		
	}
}
class InputReader {
	 
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;
 
	public InputReader(InputStream st) {
		this.stream = st;
	}
 
	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}
 
	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}
 
	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}
 
	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}
 
	public String readString() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}
 
	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}
 
	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}
 
	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}
