#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int> > v(n);
    for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
    sort(v.begin(), v.end());
    int buy = 0, ans = 0;
    priority_queue<int, vector<int>, greater<int> > pq;
    for (int i = n - 1; i >= 0; i--) {
      pq.push(v[i].second);
      if (v[i].first - i <= buy)
        continue;
      else {
        buy++;
        ans += pq.top();
        pq.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
