#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int inf = 1e9;
const int N = 2e5 + 10;
multiset<int> v[N];
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) v[i].clear();
  long long ans = 0;
  multiset<pair<int, int> > now;
  for (int i = 0; i < n; i++) {
    int p, m;
    cin >> m >> p;
    v[m].insert(p);
    now.insert(make_pair(p, -m));
  }
  int upto = 0;
  int tot = n;
  multiset<int> q;
  for (int m = n - 1; m >= 0; m--) {
    tot -= int(v[m].size());
    for (auto i : v[m]) q.insert(i);
    while (tot + upto < m) {
      upto++;
      ans += *q.begin();
      q.erase(q.begin());
    }
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
