#include <bits/stdc++.h>
using namespace std;
vector<long long int> v[200005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int q, n, x, y;
  cin >> q;
  while (q--) {
    cin >> n;
    long long int ans = 0;
    for (long long int i = 0; i < n; i++) {
      cin >> x >> y;
      v[x].push_back(y);
    }
    priority_queue<long long int> pq;
    for (long long int i = n; i >= 0; i--) {
      for (long long int j = 0; j < v[i].size(); j++) {
        pq.push(-v[i][j]);
      }
      while (pq.size() > n - i) {
        ans -= pq.top();
        pq.pop();
      }
    }
    cout << ans << endl;
    for (long long int i = 0; i < n; i++) {
      v[i].clear();
    }
  }
}
