#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
vector<int> g[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    priority_queue<int, vector<int>, greater<int> > q;
    int n, m, p;
    cin >> n;
    for (int i = 1; i <= n; i++) g[i].clear();
    for (int i = 1; i <= n; i++) {
      cin >> m >> p;
      g[m].push_back(p);
    }
    long long ans = 0;
    for (int i = n - 1; i >= 1; i--) {
      for (int j = 0; j < (int)g[i].size(); j++) q.push(g[i][j]);
      while ((int)q.size() > n - i) {
        ans += q.top();
        q.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
