#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<long long> v[N];
priority_queue<long long, vector<long long>, greater<long long>> q;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int x, y, n;
    cin >> n;
    for (int i = 0; i <= n; i++) v[i].clear();
    while (!q.empty()) q.pop();
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      v[x].push_back(y);
    }
    long long s = 0;
    for (int i = n - 1; i >= 0; i--) {
      long long len = v[i].size();
      for (int j = 0; j < len; j++) q.push(v[i][j]);
      while (q.size() > n - i) {
        s += q.top();
        q.pop();
      }
    }
    cout << s << endl;
  }
}
