#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long MOD = 123456789;
const long long inf = 2e18;
const int maxn = 200005;
const int maxm = 500005;
long long print_array(long long a[], int t) {
  cout << "[";
  for (int i = 0; i < t; i++) {
    cout << a[i];
    if (i != t - 1) cout << ", ";
  }
  cout << "]" << endl;
  return 0;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  if (b < 0) return -1;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long inv_mod(long long a) { return powmod(a, mod - 2); }
vector<int> v[maxn];
int main() {
  ios::sync_with_stdio(0);
  int t;
  cin >> t;
  int n;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      v[i].clear();
    }
    for (int i = 0; i < n; i++) {
      int x, s;
      cin >> x >> s;
      v[x].push_back(s);
    }
    multiset<int> q;
    long long res = 0;
    int pref = n;
    int cnt = 0;
    for (int i = n - 1; i >= 0; i--) {
      pref -= v[i].size();
      int need = i - pref;
      for (auto x : v[i]) q.insert(x);
      while (cnt < need) {
        ++cnt;
        res += *q.begin();
        q.erase(q.begin());
      }
    }
    cout << res << endl;
  }
}
