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