#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long SZ = 107;
const long long N = 2e5 + 1;
const long long M = 2e5 + 7;
long long pwr(long long x, long long y) {
  long long r = 1LL;
  while (y) {
    if (y & 1) r = (r * x) % MOD;
    y >>= 1;
    x = (x * x) % MOD;
  }
  return r;
}
long long inv(long long x) { return pwr(x, MOD - 2ll); }
bool comp(const pair<long long, long long> a,
          const pair<long long, long long> b) {
  if (a.first > b.first)
    return 1;
  else if (a.first == b.first and a.second < b.second)
    return 1;
  return 0;
}
pair<long long, long long> v[N];
long long n;
void solve() {
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
  sort(v, v + n, comp);
  reverse(v, v + n);
  multiset<long long> s;
  long long ans = 0, cnt = 0;
  for (long long i = n - 1; i >= 0; i--) {
    s.insert(v[i].second);
    if (v[i].first > cnt + i) {
      ans += *(s.begin());
      s.erase(s.begin());
      cnt++;
    }
  }
  cout << ans << "\n";
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
