#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> c(n);
    vector<vector<int>> vs(n);
    priority_queue<int> s;
    for (int i = 0; i < n; i++) {
      int m, p;
      cin >> m >> p;
      c[m]++;
      vs[m].push_back(-p);
    }
    long long cost = 0;
    int left = n;
    for (int i = n; i--;) {
      left -= c[i];
      for (int p : vs[i]) s.push(p);
      while (left < i) {
        cost += s.top();
        s.pop();
        left++;
      }
    }
    cout << -cost << '\n';
  }
}
