#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<long long int> > vt(n);
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      vt[x].push_back(y);
    }
    int pref = n;
    int dn = 0;
    multiset<int> ac;
    long long int ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      pref -= vt[i].size();
      for (int x : vt[i]) ac.insert(x);
      while (dn + pref < i) {
        int cr = *(ac.begin());
        ac.erase(ac.find(cr));
        ans += cr;
        dn++;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
