#include <bits/stdc++.h>
using namespace std;
long long n;
vector<pair<long long, long long> > a;
multiset<long long> s;
void solve() {
  cin >> n;
  a.resize(n), s.clear();
  for (long long i = 0; i < n; ++i) {
    cin >> a[i].first >> a[i].second;
  }
  sort(a.begin(), a.end());
  long long i = n - 1, ans = 0;
  for (long long k = n - 1; k >= 0; --k) {
    while (i >= 0 && a[i].first == k) {
      s.insert(a[i--].second);
    }
    while (n - s.size() < k) {
      ans += (*s.begin());
      s.erase(s.begin());
    }
  }
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(20);
  long long t = 1;
  cin >> t;
  while (t--) solve();
}
