#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long mod = 1e9 + 7;
bool compare(pair<long long, long long> a, pair<long long, long long> b) {
  if (a.first == b.first) return a.second < b.second;
  return a.first < b.first;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tc;
  cin >> tc;
  while (tc--) {
    long long n;
    cin >> n;
    pair<long long, long long> p[n + 1];
    long long i, j;
    for (i = 1; i <= n; ++i) cin >> p[i].first >> p[i].second;
    sort(p + 1, p + n + 1, compare);
    set<long long> pq;
    pq.clear();
    long long cost = 0, bought = 0;
    for (i = n; i >= 1; --i) {
      pq.insert(p[i].second);
      while (i - 1 + bought < p[i].first) {
        cost += (*pq.begin());
        pq.erase(pq.begin());
        ++bought;
      }
    }
    cout << cost << '\n';
  }
  return 0;
}
