import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.*;

public class ETask {
    private static final String QUICK_ANSWER = "NO";
    private final Scanner in;
    private final StringBuilder out;
    private final int n;

    public ETask(Scanner in, StringBuilder out) {
        this.in = in;
        this.out = out;
        n = nextInt();
    }

    public void solve() throws QuickAnswer {
        int[] m = new int[n];
        int[] p = new int[n];
        int[] cnt = new int[n];
        for (int i = 0; i < n; i++) {
            m[i] = nextInt();
            p[i] = nextInt();
            cnt[m[i]]++;
        }
        int[][] order = new int[n][];
        for (int i = 0; i < n; i++) {
            if(cnt[i] > 0) order[i] = new int[cnt[i]];
        }
        for (int i = 0; i < n; i++) {
            order[m[i]][--cnt[m[i]]] = p[i];
        }
        long price = 0;
        int left = n;
        int payed = 0;
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for(int i = n-1; i >= 0; --i){
            if(order[i] == null) continue;
            for(int v : order[i]) pq.add(v);
            left -= order[i].length;
            while(left + payed < i && !pq.isEmpty()){
                price += pq.poll();
                ++payed;
            }
        }
        print(price);
    }

    // Common functions

    void quickAnswer(String answer) throws QuickAnswer {
        throw new QuickAnswer(answer);
    }

    void quickAnswer() throws QuickAnswer {
        quickAnswer(QUICK_ANSWER);
    }


    static class QuickAnswer extends Exception {
        private String answer;
        public QuickAnswer(String answer) {
            this.answer = answer;
        }
    }

    void print(Object... args) {
        String prefix = "";
        for (Object arg : args) {
            out.append(prefix);
            out.append(arg);
            prefix = " ";
        }
    }

    void println(Object... args) {
        print(args);
        out.append("\n");
    }

    void printsp(Object... args) {
        print(args);
        out.append(" ");
    }

    int nextInt() {
        return in.nextInt();
    }

    long nextLong() {
        return in.nextLong();
    }

    String nextString() {
        String res = in.nextLine();
        return res.trim().isEmpty() ? in.nextLine() : res;
    }

    int[] nextInts(int count) {
        int[] res = new int[count];
        for (int i = 0; i < count; ++i) {
            res[i] = in.nextInt();
        }
        return res;
    }

    long[] nextLongs(int count) {
        long[] res = new long[count];
        for (int i = 0; i < count; ++i) {
            res[i] = in.nextInt();
        }
        return res;
    }

    public static void main(String[] args) {
        doMain(System.in, System.out);
    }

    static void doMain(InputStream inStream, PrintStream outStream) {
        Scanner in = new Scanner(new BufferedInputStream(inStream)).useLocale(Locale.ENGLISH);
        StringBuilder totalOut = new StringBuilder();
        int count = 1;
        count = in.nextInt();
        while (count-- > 0) {
            try {
                StringBuilder out = new StringBuilder();
                new ETask(in, out).solve();
                totalOut.append(out.toString());
            } catch (QuickAnswer e) {
                totalOut.append(e.answer);
            }
            if (count > 0) {
                totalOut.append("\n");
            }
        }
        outStream.print(totalOut.toString());
    }
}

