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