#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
vector<int> v[maxn];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int m, p, n;
    scanf("%d", &n);
    for (int i = 0; i <= n; i++) {
      v[i].clear();
    }
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &m, &p);
      v[m].push_back(p);
    }
    long long ans = 0;
    priority_queue<int, vector<int>, greater<int>> Q;
    for (int i = n; i >= 0; i--) {
      int len = v[i].size();
      for (int j = 0; j < len; j++) {
        Q.push(v[i][j]);
      }
      while (Q.size() > n - i) {
        ans += Q.top();
        Q.pop();
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
