#include <bits/stdc++.h>
using namespace std;
signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<pair<long long, long long>> arr(n);
    for (long long i = 0; i < n; i++) {
      cin >> arr[i].first >> arr[i].second;
    }
    sort(arr.begin(), arr.end());
    long long total = 0;
    for (long long i = 0; i < n; i++) {
      total += arr[i].second;
    }
    long long total2 = 0;
    priority_queue<long long> q;
    long long ind = 0;
    for (long long i = 0; i < n; i++) {
      while (ind < n && arr[ind].first == i) {
        q.push(arr[ind].second);
        ind++;
      }
      if (q.size()) {
        long long v = q.top();
        q.pop();
        total2 += v;
      }
    }
    cout << total - total2 << "\n";
  }
  return 0;
}
