#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const double pi = acos((double)-1);
const long long MAXN = 2e5;
const long long mod = 1e9 + 7;
class Node {
 public:
  long long m, p;
  bool operator<(const Node &x) const {
    if (m == x.m) return p < x.p;
    return m < x.m;
  }
} a[MAXN + 5];
priority_queue<int, vector<int>, greater<int> > Q;
int main() {
  a[0].m = a[0].p = -1;
  int T;
  scanf("%d", &T);
  while (T--) {
    while (!Q.empty()) Q.pop();
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i].m >> a[i].p;
    sort(a, a + n + 1);
    int dp = n;
    long long ans = 0;
    long long sum = 0;
    for (int i = n; i >= 0; i--) {
      if (a[i].m < dp) {
        for (int j = 0; j < dp - i - sum; j++) {
          ans += Q.top();
          Q.pop();
        }
        sum += dp - i - sum;
        dp = a[i].m;
      }
      Q.push(a[i].p);
    }
    cout << ans << "\n";
  }
  return 0;
}
