#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 5;
struct N {
  int m, p;
} p[M];
multiset<int> s;
bool cmp(N x, N y) {
  if (x.m != y.m) return x.m < y.m;
  return x.p < y.p;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    s.clear();
    for (int i = 0; i < n; i++) {
      int m, q;
      scanf("%d%d", &m, &q);
      p[i] = {m, q};
    }
    sort(p, p + n, cmp);
    long long res = 0;
    int have = 0;
    for (int i = n - 1; i >= 0; i--) {
      int need = p[i].m - i;
      s.insert(p[i].p);
      while (!s.empty() && have < need) {
        res += *(s.begin());
        s.erase(s.begin());
        have++;
      }
    }
    printf("%lld\n", res);
  }
  return 0;
}
