#include <bits/stdc++.h>
using namespace std;
const long o = 200005;
long n;
pair<long, long> a[o];
multiset<long> s;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long test;
  cin >> test;
  a[0].first = -1;
  while (test--) {
    cin >> n;
    for (long i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
    sort(a + 1, a + 1 + n);
    long sl = 0;
    long long kq = 0;
    while (!s.empty()) s.erase(s.begin());
    for (long i = n; i >= 1; i--) {
      s.insert(a[i].second);
      if (a[i].first != a[i - 1].first && i - 1 + sl < a[i].first) {
        while (i - 1 + sl < a[i].first) {
          sl++;
          kq += *s.begin();
          s.erase(s.begin());
        }
      }
    }
    cout << kq << '\n';
  }
  return 0;
}
