#include <bits/stdc++.h>
using namespace std;
long long M = 1e9 + 7;
double pi = acos(-1.0);
long long powerm(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = (res * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return res % M;
}
long long power(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
long long gcd(long long a, long long b) {
  if (b > a) return gcd(b, a);
  if (b == 0) return a;
  return gcd(b, a % b);
}
const int N = 2e5 + 5;
vector<int> v[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 0; i < n + 1; i++) v[i].clear();
    for (int i = 0; i < n; i++) {
      int m;
      long long p;
      cin >> m >> p;
      v[m].push_back(p);
    }
    long long cc = 0, ans = 0, pref = n;
    multiset<long long> s;
    for (long long i = n - 1; i >= 0; i--) {
      if (v[i].empty()) continue;
      pref -= v[i].size();
      for (auto it : v[i]) s.insert(it);
      while (pref + cc < i) {
        cc++;
        ans += *s.begin();
        s.erase(s.begin());
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
