#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int q;
  cin >> q;
  while (q--) {
    long long n, cnt = 0, res = 0, i;
    cin >> n;
    vector<pair<long long, long long>> a(n);
    for (i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
    sort(a.begin(), a.end());
    priority_queue<long long> x;
    for (i = n - 1; i >= 0; i--) {
      x.push(-a[i].second);
      if (cnt + i < a[i].first) {
        res -= x.top();
        x.pop();
        cnt++;
      }
    }
    cout << res << endl;
  }
  return 0;
}
