#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    long long n;
    cin >> n;
    vector<pair<int, int>> v(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i].first >> v[i].second;
      v[i].second = -v[i].second;
    }
    sort(v.begin(), v.end());
    int cnt = 0;
    long long ans = 0;
    priority_queue<int> que;
    for (int i = n - 1; i >= 0; i--) {
      que.push(v[i].second);
      while (cnt < v[i].first - i) {
        cnt++;
        ans -= que.top();
        que.pop();
      }
    }
    cout << ans << endl;
  }
}
