#include <bits/stdc++.h>
using namespace std;
int Gcd(int a, int b) {
  if (b == 0) return a;
  return Gcd(b, a % b);
}
int Lcm(int a, int b) { return a / Gcd(a, b) * b; }
template <class T>
void read(T &x) {
  static char ch;
  static bool neg;
  for (ch = neg = 0; ch < '0' || '9' < ch; neg |= ch == '-', ch = getchar())
    ;
  for (x = 0; '0' <= ch && ch <= '9'; (x *= 10) += ch - '0', ch = getchar())
    ;
  x = neg ? -x : x;
}
const long long maxn = 2e5 + 10;
struct node {
  long long m, p;
  bool operator<(const node &rhs) const { return m < rhs.m; }
} v[maxn];
vector<long long> prc[maxn];
multiset<long long> ms;
long long pre[maxn];
signed main() {
  long long T;
  read(T);
  while (T--) {
    long long n;
    ms.clear();
    read(n);
    for (long long i = 1; i <= n; i++) {
      read(v[i].m);
      read(v[i].p);
      pre[v[i].m]++;
      prc[v[i].m].push_back(v[i].p);
    }
    for (long long i = 1; i <= n; i++) pre[i] += pre[i - 1];
    sort(v + 1, v + n + 1);
    long long ans = 0, cnt = 0;
    for (long long i = n; i >= 1; i--) {
      for (auto now : prc[i]) ms.insert(now);
      long long need = i - pre[i - 1] - cnt;
      for (long long j = 0; j < need; j++) {
        cnt++;
        ans += *ms.begin();
        ms.erase(ms.begin());
      }
    }
    cout << ans << endl;
    for (long long i = 0; i <= n; i++) {
      prc[v[i].m].clear();
      pre[i] = 0;
    }
  }
  return 0;
}
