import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Objects;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author anand.oza
 */
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);
        E1VotingEasyVersion solver = new E1VotingEasyVersion();
        solver.solve(1, in, out);
        out.close();
    }

    static class E1VotingEasyVersion {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int t = in.nextInt();
            for (int i = 0; i < t; i++) {
                solve(in, out);
            }
        }

        private void solve(InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] m = new int[n];
            int[] p = new int[n];
            MPair<Integer, Integer>[] votes = new MPair[n];
            for (int i = 0; i < n; i++) {
                m[i] = in.nextInt();
                p[i] = in.nextInt();
                votes[i] = MPair.of(m[i], p[i]);
            }

            Arrays.sort(votes, Comparator.comparingInt((MPair<Integer, Integer> i) -> i.first).thenComparingInt(i -> i.second));

            for (int i = 0, j = 0, tot = 0; i < n; i = j) {
                while (j < n && votes[i].first.equals(votes[j].first))
                    j++;

                if (votes[i].first <= tot) {
                    for (int k = i; k < j; k++)
                        votes[k].first = i > 0 ? votes[i - 1].first : 0;
                }

                tot += j - i;
            }

            Arrays.sort(votes, Comparator.comparingInt((MPair<Integer, Integer> i) -> i.first).thenComparingInt(i -> i.second));

            long answer = 0;
            for (int i = n - 1, extra = 0; i >= 0; i--) {
                if (votes[i].first > i + extra) {
                    answer += votes[i].second;
                    extra++;
                }
            }

            out.println(answer);
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

    }

    static class MPair<F, S> {
        public F first;
        public S second;

        public MPair(F first, S second) {
            this.first = first;
            this.second = second;
        }

        public static <F, S> MPair<F, S> of(F first, S second) {
            return new MPair<>(first, second);
        }

        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;
            MPair<?, ?> pair = (MPair<?, ?>) o;
            return Objects.equals(first, pair.first) && Objects.equals(second, pair.second);
        }

        public int hashCode() {
            return Objects.hash(first, second);
        }

        public String toString() {
            return "(" + first + ", " + second + ')';
        }

    }
}

