#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
vector<int> ps[200000];
int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) ps[i].clear();
    for (int i = 0; i < n; i++) {
      int p, m;
      cin >> m >> p;
      ps[m].push_back(p);
    }
    priority_queue<int> Q;
    for (int x = 0; x < n; x++) {
      for (int p : ps[x]) Q.push(p);
      if (!Q.empty()) Q.pop();
    }
    long long sum = 0;
    while (!Q.empty()) {
      sum += Q.top();
      Q.pop();
    }
    cout << sum << endl;
  }
}
