#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long INF = 1e18;
const int M = 1e9 + 7;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    std::vector<vector<int> > v(n + 1);
    for (int i = 0; i < n; ++i) {
      int mi, pi;
      cin >> mi >> pi;
      v[mi].emplace_back(pi);
    }
    priority_queue<int> pq;
    long long res = 0;
    for (int i = n; i >= 0; --i) {
      for (int j : v[i]) {
        pq.push(-j);
      }
      while ((int)pq.size() > n - i) {
        res -= pq.top();
        pq.pop();
      }
    }
    cout << res << endl;
  }
  return 0;
}
