#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 10;
int n;
long long int ans;
vector<int> vt[M];
priority_queue<int> pq;
int main(int argc, char const *argv[]) {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      int p, m;
      scanf("%d%d", &m, &p);
      vt[m].push_back(p);
    }
    for (int i = n - 1; i >= 0; i--) {
      for (int j = 0; j < vt[i].size(); j++) pq.push(-vt[i][j]);
      while (pq.size() > n - i) ans -= pq.top(), pq.pop();
    }
    printf("%lld\n", ans);
    ans = 0;
    while (!pq.empty()) pq.pop();
    for (int i = 0; i <= n - 1; i++) vt[i].clear();
  }
  return 0;
}
