#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool scan(T& ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF) return 0;
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
const long long mod = 1e9 + 7;
const int maxn = 2e5 + 50;
long long qp(long long x, long long n) {
  long long res = 1;
  x %= mod;
  while (n > 0) {
    if (n & 1) res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
int n;
vector<int> a[maxn];
struct node {
  int x;
  bool operator<(const node& oth) const { return x > oth.x; }
};
int main(int argc, char* argv[]) {
  int T;
  scanf("%d", &T);
  for (int kase = 1; kase <= T; ++kase) {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) a[i].clear();
    for (int i = 0; i < n; ++i) {
      int x, y;
      scanf("%d%d", &x, &y);
      a[x].push_back(y);
    }
    for (int i = 0; i < n; ++i) sort(a[i].begin(), a[i].end());
    int pre = n;
    priority_queue<node> p;
    long long res = 0;
    int has = 0;
    for (int i = n - 1; i >= 0; --i) {
      pre -= a[i].size();
      int need = i - pre;
      for (auto j : a[i]) p.push({j});
      while (has < need) {
        has++;
        res += p.top().x;
        p.pop();
      }
    }
    printf("%lld\n", res);
  }
  return 0;
}
