#include <bits/stdc++.h>
using namespace std;
vector<int> V[200005];
priority_queue<int> q;
long long ans;
int T, n;
int x, y;
int main() {
  scanf("%d", &T);
  while (T-- > 0) {
    scanf("%d", &n);
    for (int i = 0; i <= n; i++) V[i].clear();
    while (!q.empty()) q.pop();
    ans = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &x, &y);
      V[x].push_back(y);
    }
    for (int i = n - 1; i >= 0; i--) {
      for (int j = 0; j < V[i].size(); j++) q.push(-V[i][j]);
      while (n - q.size() < i) {
        ans -= q.top();
        q.pop();
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
