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

public class LabAlgoOne {
    //Scanner

    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));
    static String currentInputLine = "";
    static String inputFileName = "avia.in";
    static String outputFileName = "avia.out";
    static int currentInputIndex = 0;

    static void nextInputLine() {
        try {

            currentInputLine = reader.readLine();
            currentInputIndex = 0;
        } catch (IOException ignored) {
            throw new RuntimeException();
        }
    }

    static void skipSpaces() {
        while (currentInputIndex < currentInputLine.length() && currentInputLine.charAt(currentInputIndex) == ' ') {
            currentInputIndex++;
        }
        if (currentInputIndex == currentInputLine.length()) {
            nextInputLine();
            skipSpaces();
        }
    }

    static String next() {
        skipSpaces();
        int end = currentInputLine.indexOf(" ", currentInputIndex);
        if (end == -1) {
            end = currentInputLine.length();
        }
        String res = currentInputLine.substring(currentInputIndex, end);
        currentInputIndex = end;
        return res;
    }

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

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

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

    static void toFile() {
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFileName)));
            writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputFileName)));
        } catch (IOException e) {
            throw new RuntimeException();
        }
    }

    //Main

    private static class Worker {
        long min, max;

        Worker(long min, long max) {
            this.min = min;
            this.max = max;
        }
    }

    private static boolean check(long sum, long med, ArrayList<Worker> workers) {
        ArrayList<Worker> less = new ArrayList<>();
        ArrayList<Worker> proc = new ArrayList<>();
        ArrayList<Worker> great = new ArrayList<>();
        for (Worker worker : workers) {
            if (worker.max < med) {
                less.add(worker);
            } else if (worker.min > med) {
                great.add(worker);
            } else {
                proc.add(worker);
            }
        }
        if (less.size() > workers.size() / 2 || great.size() > workers.size() / 2) {
            return false;
        }
        proc.sort(new Comparator<Worker>() {
            @Override
            public int compare(Worker worker, Worker t1) {
                long res = worker.min - t1.min;
                if (res < 0) {
                    return -1;
                } else if (res == 0) {
                    return 0;
                } else {
                    return 1;
                }
            }
        });
        long sup = 0;
        for (Worker worker : less) {
            sup += worker.min;
        }
        for (Worker worker : great) {
            sup += worker.min;
        }
        sup += (workers.size() / 2 + 1 - great.size()) * med;
        for (int i = 0; i + less.size() < workers.size() / 2; i++) {
            sup += proc.get(i).min;
        }
        return sup <= sum;
    }

    public static void main(String[] args) {
        int n = nextInt();
        for (int i = 0; i < n; i++) {
            int m = nextInt();
            ArrayList<ArrayList<Long>> list = new ArrayList<>();
            ArrayList<Integer> prefSum = new ArrayList<>();
            for (int j = 0; j < m; j++) {
                list.add(new ArrayList<>());
            }
            for (int j = 0; j < m; j++) {
                int num = nextInt();
                long cost = nextLong();
                list.get(num).add(cost);
            }
            prefSum.add(0);
            for (int j = 1; j < m; j++) {
                prefSum.add(prefSum.get(j - 1) + list.get(j - 1).size());
            }
            int count = 0;
            long res = 0;
            PriorityQueue<Long> tail = new PriorityQueue<>();
            for (int j = m - 1; j >= 0; j--) {
                tail.addAll(list.get(j));
                while (prefSum.get(j) + count < j) {
                    count++;
                    res += tail.poll();
                }
            }
            System.out.println(res);
        }
        writer.println();
        writer.close();
    }
}
