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