#include <bits/stdc++.h>
const long long mod = 1000000007;
const long long lim = 1e18;
const double PI = acos(-1.);
using namespace std;
long long binpow(long long a, long long n) {
  if (n == 0) return 1;
  if (n == 1) return a;
  long long x = binpow(a, n / 2);
  if (n % 2 == 0) return x * x;
  return x * x * a;
}
const long long MAXN = 2e5 + 10;
vector<long long> cnt[MAXN];
long long t, n, temp, p;
multiset<long long> ms;
signed main() {
  cin >> t;
  while (t--) {
    ms.clear();
    cin >> n;
    for (long long i = 0; i < n; i++) cnt[i].clear();
    for (long long i = 0; i < n; i++) {
      cin >> temp >> p;
      cnt[temp].push_back(p);
    }
    long long ans = 0;
    long long buyvoted = 0, prefix = n;
    for (long long i = n - 1; i >= 0; i--) {
      prefix -= cnt[i].size();
      long long needtobribe = i - prefix;
      for (auto j : cnt[i]) ms.insert(j);
      while (buyvoted < needtobribe) {
        ans += *ms.begin();
        ms.erase(ms.begin());
        buyvoted++;
      }
    }
    cout << ans << '\n';
  }
}
