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.Comparator;
import java.util.Collections;
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 {
        final long INF = 1L << 60;

        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));
                }
                ArrayList<Item> remList = new ArrayList<>();
                TreeSet<Item> set = new TreeSet<>(new Comparator<Item>() {

                    public int compare(Item o1, Item o2) {
                        if (o1.p != o2.p) return o1.p < o2.p ? -1 : 1;
                        if (o1.m != o2.m) return o1.m < o2.m ? -1 : 1;
                        if (o1.id != o2.id) return o1.id < o2.id ? -1 : 1;
                        return 0;
                    }
                });
                for (int i = 0; i < n + 1; i++) {
                    Collections.sort(list[i]);
                    int size = list[i].size();
                    if (size == 0) continue;
                    for (int j = 0; j < size - 1; j++) {
                        set.add(list[i].get(j));
                    }
                    remList.add(list[i].get(size - 1));
                }

                long ans = INF;
                for (int i = 0; i <= remList.size(); i++) {
                    int cur = i;
                    TreeSet<Item> curSet = new TreeSet<>();
                    for (Item t : set) curSet.add(t);
                    for (int j = remList.size() - 1; cur-- > 0; j--) {
                        curSet.add(remList.get(j));
                    }
                    long curAns = 0;
                    boolean ok = true;
                    int voted = 0;
                    int[] cnt = new int[n + 1];
                    for (int j = 0; j < n + 1; j++)
                        cnt[j] = list[j].size();
                    for (int j = 0; ok && j < remList.size(); j++) {
                        while (remList.get(j).m > voted) {
                            if (curSet.isEmpty()) {
                                ok = false;
                                break;
                            }
                            Item last = curSet.pollFirst();
                            cnt[last.m]--;
                            curAns += last.p;
                            voted++;
                        }
                        voted += cnt[remList.get(j).m];
                    }
                    if (ok == true) {
                        ans = Math.min(ans, curAns);
                    }
                }
                out.println(ans);
            }
        }

        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();
            }
        }

    }
}

