#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, i, m, p;
    cin >> n;
    vector<long long int> a[n + 1];
    for (i = 0; i < n; i++) {
      cin >> m >> p;
      a[m].push_back(p);
    }
    priority_queue<long long int> q;
    long long int ans = 0;
    for (i = n; i >= 0; i--) {
      for (long long int p1 : a[i]) {
        q.push(-p1);
      }
      while (q.size() > n - i) {
        ans -= q.top();
        q.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
