#include <bits/stdc++.h>
using namespace std;
vector<int> V[200005];
priority_queue<int, vector<int>, greater<int> > Q;
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);
    }
    while (!Q.empty()) Q.pop();
    long long ans = 0;
    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 ((int)Q.size() > n - i) ans += Q.top(), Q.pop();
    }
    printf("%lld\n", ans);
  }
}
