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

import static java.lang.Math.max;
import static java.lang.Math.min;

public class Main {


    void run() throws IOException {
        int t = nextInt();
        while (t-- > 0) {
            int n = nextInt();
            long dp[][] = new long[n][n];
            Point[] a = new Point[n];
            for (int i = 0; i < a.length; i++) {
                a[i] = new Point(nextLong(), nextInt());
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    dp[i][j] = Long.MAX_VALUE / 2;
                }
            }
            Arrays.sort(a, new PointComp());
            Arrays.fill(dp[0], 0);
            for (int i = 0; i < n; i++) {
                if (a[0].m > i) dp[0][i] = a[0].pay;
            }
            for (int i = 1; i < n; i++) {
                for (int j = 0; j < n - i; j++) {
                    if (a[i].m <= j + i) dp[i][j] = dp[i - 1][j];
                    dp[i][j] = min(dp[i][j], dp[i - 1][j + 1] + a[i].pay);
                }
            }
            pw.println(dp[n - 1][0]);
        }
        pw.close();
    }

    class Point {
        long m, pay;

        public Point(long a, long b) {
            m = a;
            pay = b;
        }
    }

    class PointComp implements Comparator<Point> {

        @Override
        public int compare(Point o1, Point o2) {
            if (o1.m == o2.m) return Long.compare(o1.pay, o2.pay);
            return Long.compare(o1.m, o2.m);
        }
    }

    long mod = (long) 1e9 + 7;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    //BufferedReader br = new BufferedReader(new FileReader("qual.in"));

    StringTokenizer st = new StringTokenizer("");
    PrintWriter pw = new PrintWriter(System.out);
    //PrintWriter pw = new PrintWriter("qual.out");

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    String next() throws IOException {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    public Main() throws FileNotFoundException {
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
}