#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long q;
  cin >> q;
  long long n;
  vector<pair<long long, long long> > a;
  long long x, y;
  set<long long> d;
  long long curr;
  long long sum;
  for (long long i = 0; i < q; ++i) {
    cin >> n;
    a.resize(n);
    d.clear();
    curr = n;
    sum = 0;
    for (long long i = 0; i < n; ++i) {
      cin >> x >> y;
      a[i] = {x, y};
    }
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    for (long long i = 0; i < n; ++i) {
      d.insert(a[i].second);
      if (a[i].first >= curr) {
        sum += (*d.begin());
        d.erase(d.begin());
        curr++;
      }
      curr--;
    }
    cout << sum << endl;
  }
  return 0;
}
