// Working program using Reader Class 
import java.io.BufferedReader;
import java.io.DataInputStream; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;


public class Main1 
{ 
	static class Reader 
	{ 
		final private int BUFFER_SIZE = 1 << 16; 
		private DataInputStream din; 
		private byte[] buffer; 
		private int bufferPointer, bytesRead; 

		public Reader() 
		{ 
			din = new DataInputStream(System.in); 
			buffer = new byte[BUFFER_SIZE]; 
			bufferPointer = bytesRead = 0; 
		} 

		public Reader(String file_name) throws IOException 
		{ 
			din = new DataInputStream(new FileInputStream(file_name)); 
			buffer = new byte[BUFFER_SIZE]; 
			bufferPointer = bytesRead = 0; 
		} 

		public String readLine() throws IOException 
		{ 
			byte[] buf = new byte[64]; // line length 
			int cnt = 0, c; 
			while ((c = read()) != -1) 
			{ 
				if (c == '\n') 
					break; 
				buf[cnt++] = (byte) c; 
			} 
			return new String(buf, 0, cnt); 
		} 

		public int nextInt() throws IOException 
		{ 
			int ret = 0; 
			byte c = read(); 
			while (c <= ' ') 
				c = read(); 
			boolean neg = (c == '-'); 
			if (neg) 
				c = read(); 
			do
			{ 
				ret = ret * 10 + c - '0'; 
			} while ((c = read()) >= '0' && c <= '9'); 

			if (neg) 
				return -ret; 
			return ret; 
		} 

		public long nextLong() throws IOException 
		{ 
			long ret = 0; 
			byte c = read(); 
			while (c <= ' ') 
				c = read(); 
			boolean neg = (c == '-'); 
			if (neg) 
				c = read(); 
			do { 
				ret = ret * 10 + c - '0'; 
			} 
			while ((c = read()) >= '0' && c <= '9'); 
			if (neg) 
				return -ret; 
			return ret; 
		} 

		public double nextDouble() throws IOException 
		{ 
			double ret = 0, div = 1; 
			byte c = read(); 
			while (c <= ' ') 
				c = read(); 
			boolean neg = (c == '-'); 
			if (neg) 
				c = read(); 

			do { 
				ret = ret * 10 + c - '0'; 
			} 
			while ((c = read()) >= '0' && c <= '9'); 

			if (c == '.') 
			{ 
				while ((c = read()) >= '0' && c <= '9') 
				{ 
					ret += (c - '0') / (div *= 10); 
				} 
			} 

			if (neg) 
				return -ret; 
			return ret; 
		} 

		private void fillBuffer() throws IOException 
		{ 
			bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); 
			if (bytesRead == -1) 
				buffer[0] = -1; 
		} 

		private byte read() throws IOException 
		{ 
			if (bufferPointer == bytesRead) 
				fillBuffer(); 
			return buffer[bufferPointer++]; 
		} 

		public void close() throws IOException 
		{ 
			if (din == null) 
				return; 
			din.close(); 
		} 
	} 

	static long pow(long a, long b , long c){
        if(b==0){
            return 1;
        }
        long d=pow(a, b/2, c);
        d=(d*d)%c;
        if(b%2==1){
            d=(d*a)%c;
        }
        return d;
	}

	static int gcd(int a, int b){
		if(b==0){
			return a;
		}
		return gcd(b, a%b);
	}

	static class Node{
		int id, coins, num;
		Node(int a, int b, int c){
			id=a;
			num=b;
			coins=c;
		}
	}
	public static void main(String[] args) throws IOException 
	{ 
		Reader s=new Reader();
		int t=s.nextInt();
		while(t-->0){
			int n=s.nextInt(), strength=0, i, l;
			long money=0L;
			boolean[] bought = new boolean[n];
			PriorityQueue<Node> pq1 = new PriorityQueue<>(10, new Comparator<Node>() {
				public int compare(Node n1, Node n2){
					if(n1.num==n2.num){
						return n1.coins-n2.coins;
					}
					return n1.num-n2.num;
				}
			});
			PriorityQueue<Node> pq2 = new PriorityQueue<>(10, new Comparator<Node>() {
				public int compare(Node n1, Node n2){
					if(n1.coins==n2.coins){
						return n2.num-n1.num;
					}
					return n1.coins-n2.coins;
				}
			});
			Node node = new Node(0, 0, 0);
			for(i=0;i<n;i++){
				node = new Node(i, s.nextInt(), s.nextInt());
				pq1.add(node);
				pq2.add(node);
			}
			while(pq1.size()>0 || pq2.size()>0){
				l=0;
				if(pq1.size()==0){
					l=1;
				}
				else if(pq2.size()==0){
					l=0;
				}
				else{
					if(strength>=pq1.peek().num){
						l=0;
					}
					else{
						l=1;
					}
				}
				if(l==0){
					node=pq1.poll();
					if(!bought[node.id]){
						bought[node.id]=true;
						if(strength<node.num){
							money=money+1L*node.coins;
						}
						strength++;
					}
				}
				else{
					node=pq2.poll();
					if(!bought[node.id]){
						while(pq2.size()>0 && bought[pq2.peek().id]){
							pq2.poll();
						}
						if(strength<node.num){
							if(pq2.size()>0 && pq2.peek().num>strength+1 && strength+1==node.num){
								bought[pq2.peek().id]=true;
								money+=1L*pq2.peek().coins;
								pq2.poll();
								pq2.add(node);
							}
							else{
								bought[node.id]=true;
								money+=1L*node.coins;
							}
						}
						strength++;
					}
				}
				while(pq1.size()>0 && bought[pq1.peek().id]){
					pq1.poll();
				}
				while(pq2.size()>0 && bought[pq2.peek().id]){
					pq2.poll();
				}
			}
			System.out.println(money);
		}
	}
}