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