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