#include <bits/stdc++.h>
using namespace std;
int T;
priority_queue<int, vector<int>, greater<int> > q;
vector<int> g[1000005];
int main() {
  while (scanf("%d", &T) != EOF) {
    int n;
    while (T--) {
      long long int ans = 0;
      scanf("%d", &n);
      int x, y;
      for (int i = 1; i <= n; i++) {
        scanf("%d %d", &x, &y);
        g[x].push_back(y);
      }
      for (int i = n - 1; i >= 0; i--) {
        int sz = g[i].size();
        for (int j = 0; j < sz; j++) q.push(g[i][j]);
        while (q.size() > n - i) {
          printf("top = %d\n", q.top());
          ans += q.top(), q.pop();
        }
      }
      printf("%lld\n", ans);
      for (int i = 0; i < n; ++i) {
        g[i].clear();
        while (!q.empty()) q.pop();
      }
    }
  }
  return 0;
}
