#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
long long modpow(long long a, long long b,
                 long long mod = (long long)(1e9 + 7)) {
  if (!b) return 1;
  a %= mod;
  return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2e5 + 19;
vector<int> v[N];
int n, t;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> t;
  while (t-- > 0) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      v[i].clear();
    }
    for (int i = 0; i < n; i++) {
      int z, y;
      cin >> z >> y;
      v[z].push_back(y);
    }
    multiset<int> s;
    int pref = n;
    long long sum = 0;
    long long cnt = 0;
    for (int i = n - 1; i >= 0; i--) {
      pref -= v[i].size();
      if (pref + cnt >= v[i].size()) {
        cnt += v[i].size();
        continue;
      }
      for (int k : v[i]) s.insert(k);
      while (pref + cnt < v[i].size()) {
        cnt++;
        sum += *s.begin();
        s.erase(s.begin());
      }
    }
    cout << sum << endl;
  }
  return 0;
}
