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

public class Main
{
	PrintWriter out = new PrintWriter(System.out);
	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer tok = new StringTokenizer("");
    String next() throws IOException {
        if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); }
        return tok.nextToken();
    }
    int ni() throws IOException { return Integer.parseInt(next()); }
    long nl() throws IOException { return Long.parseLong(next()); }
    
    static long[][]A=new long[5001][5001];
    static int[][]M;
    static int n;
    static long mv;
        
    void solve() throws IOException {
        for (int TC=ni();TC>0;TC--) {
            n=ni();
            mv=1000000000*(long)n;
            M=new int[n+1][2];
            for (int x=1;x<=n;x++) {
                M[x][0]=ni();
                M[x][1]=ni();
                Arrays.fill(A[x],-1);
            }
            Arrays.sort(M,(a,b)->{ if (a[0]==b[0]) return a[1]-b[1]; else return a[0]-b[0]; });
            
            out.println(DP(1,0));
        }
        out.flush();
    }
    
    long DP (int v, int r) {
        if (v==n) {
            if (r>1) return (mv);
            if (r==1 || M[v][0]==n) return M[v][1];
            return 0;
        }
        if (A[v][r]==-1) {
            if (r>n-v+1) A[v][r]=mv;
            else A[v][r]=Math.min(DP(v+1,(r+v-1>=M[v][0]?r:Math.max(r,M[v][0]-v+1))),DP(v+1,(r>0?r-1:0))+M[v][1]);
        }
        return A[v][r];
    }
    
    public static void main(String[] args) throws IOException {
        new Main().solve();
    }
}