#include <bits/stdc++.h>
using namespace std;
long long powmod(long long base, long long exponent, long long mod) {
  long long ans = 1;
  while (exponent) {
    if (exponent & 1) ans = (ans * base) % mod;
    base = (base * base) % mod;
    exponent /= 2;
  }
  return ans;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
const int upperlimit = 2e5 + 1;
const int mod = 1e9 + 7;
vector<int> prices[upperlimit];
int pref[upperlimit];
priority_queue<int> pq;
int main() {
  int t, m, n, p;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &m);
      scanf("%d", &p);
      prices[m].push_back(p);
    }
    pref[0] = prices[0].size();
    long long sum = 0;
    int buyed = 0;
    for (int i = 1; i < n; i++) {
      pref[i] = pref[i - 1] + prices[i].size();
    }
    for (int i = n - 1; i > 0; i--) {
      int k = 0;
      k = pref[i - 1];
      k += buyed;
      int temp = i - k;
      for (int j = 0; j < prices[i].size(); j++) pq.push(-prices[i][j]);
      for (int i = 1; i <= temp; i++) {
        sum -= pq.top();
        pq.pop();
        buyed++;
      }
    }
    printf("%lld\n", sum);
    for (int i = 0; i < n; i++) {
      prices[i].clear();
      pref[i] = 0;
    }
    while (!pq.empty()) pq.pop();
  }
  return 0;
}
