import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.TreeSet;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author beginner1010
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE1 solver = new TaskE1();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE1 {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int tests = in.nextInt();
            for (int testCase = 0; testCase < tests; testCase++) {
                int n = in.nextInt();
                ArrayList<Item>[] list = new ArrayList[n + 1];
                for (int i = 0; i < n + 1; i++)
                    list[i] = new ArrayList<>();
                for (int i = 0; i < n; i++) {
                    int m = in.nextInt();
                    int p = in.nextInt();
                    list[m].add(new Item(p, m, i));
                }

                TreeSet<Item> set = new TreeSet<>();
                int voted = n;
                int purchased = 0;
                long coins = 0;
                for (int i = n; i >= 0; i--) {
                    voted -= list[i].size();
                    for (Item t : list[i]) set.add(t);
                    int need = i - voted - purchased;
                    while (set.isEmpty() == false && need > 0) {
                        coins += set.pollFirst().p;
                        need--;
                        purchased++;
                    }
                }
                out.println(coins);
            }
        }

        class Item implements Comparable<Item> {
            int p;
            int m;
            int id;

            Item(int _p, int _m, int _id) {
                this.p = _p;
                this.m = _m;
                this.id = _id;
            }


            public int compareTo(Item o) {
                if (this.p != o.p) return this.p < o.p ? -1 : 1;
                if (this.m != o.m) return this.m < o.m ? -1 : 1;
                if (this.id != o.id) return this.id < o.id ? -1 : 1;
                return 0;
            }

        }

    }

    static class InputReader {
        InputStream is;
        private byte[] inbuf = new byte[1024];
        private int lenbuf = 0;
        private int ptrbuf = 0;
        static final int[] ints = new int[128];

        public InputReader(InputStream is) {
            for (int i = '0'; i <= '9'; i++) ints[i] = i - '0';
            this.is = is;
        }

        public int readByte() {
            if (lenbuf == -1) throw new InputMismatchException();
            if (ptrbuf >= lenbuf) {
                ptrbuf = 0;
                try {
                    lenbuf = is.read(inbuf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (lenbuf <= 0) return -1;
            }
            return inbuf[ptrbuf++];
        }

        public int nextInt() {
            int num = 0, b;
            boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-') {
                minus = true;
                b = readByte();
            }

            while (true) {
                if (b >= '0' && b <= '9') {
                    num = (num << 3) + (num << 1) + ints[b];
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }

    }
}

