#include <bits/stdc++.h>
using namespace std;
int t, n;
pair<int, int> v[200002];
int main() {
  if (1) ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> t;
  for (; t; --t) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> v[i].first >> v[i].second;
    sort(v + 1, v + n + 1);
    multiset<int> s;
    long long cost = 0;
    int tt = 0;
    for (int i = n; i >= 1; --i) {
      s.insert(v[i].second);
      if (i + tt <= v[i].first) {
        ++tt;
        cost += *s.begin();
        s.erase(s.begin());
      }
    }
    cout << cost << '\n';
  }
  return 0;
}
