#include <bits/stdc++.h>
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-6;
template <class T>
inline void read(T &x) {
  int sign = 1;
  char c = getchar();
  x = 0;
  while (c > '9' || c < '0') {
    if (c == '-') sign = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= sign;
}
using namespace std;
const int maxn = 2e5 + 10;
vector<int> v[maxn];
int main() {
  int t;
  for (read(t); t--;) {
    int n;
    int m, p;
    read(n);
    for (int i = 0; i <= n; ++i) {
      v[i].clear();
    }
    for (int i = 0; i < n; ++i) {
      read(m), read(p);
      v[m].push_back(p);
    }
    priority_queue<int, vector<int>, greater<int> > pq;
    long long res = 0;
    for (int i = n; i >= 0; --i) {
      for (auto &x : v[i]) pq.push(x);
      while (pq.size() > n - i) {
        res += pq.top();
        pq.pop();
      }
    }
    printf("%lld\n", res);
  }
}
