#include <bits/stdc++.h>
using namespace std;
const int maxn = 200050;
struct node {
  int p;
  bool operator<(const node &x) const { return p > x.p; }
} a[maxn];
const long long mod = 1e9 + 7;
priority_queue<node> Q;
vector<int> V[maxn];
int main() {
  int t, n, x;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    int mx = 0;
    for (int i = 0; i <= n; i++) V[i].clear();
    while (!Q.empty()) Q.pop();
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &x, &a[i].p);
      mx = max(mx, x);
      V[x].push_back(i);
    }
    long long ans = 0;
    for (int i = mx; i >= 0; i--) {
      for (auto it : V[i]) Q.push(a[it]);
      while (Q.size() > n - i) {
        ans += Q.top().p;
        Q.pop();
      }
    }
    printf("%lld\n", ans);
  }
}
