#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1.0);
vector<long long> g[N];
int main() {
  std::ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) g[i].clear();
    for (int i = 1; i <= n; i++) {
      long long m, p;
      cin >> m >> p;
      g[m].push_back(p);
    }
    priority_queue<long long, vector<long long>, greater<long long> > pq;
    int cnt = 0;
    long long ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      int sz = g[i].size();
      for (int j = 0; j < sz; j++) {
        pq.push(g[i][j]);
      }
      while (n - pq.size() < i) {
        ans += pq.top();
        pq.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
