#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const long long mod = 1e9 + 7;
const long long N = 2e5 + 5, K = 105;
const int si = (1 << 20);
const long long add = 1e4;
const long long M = 100 + 5;
const double INFdb = 1e10;
int t;
int n;
vector<int> a[N];
priority_queue<int, vector<int>, greater<int> > pq;
void reset() {
  for (int i = 0; i < n; ++i) a[i].clear();
  while (!pq.empty()) pq.pop();
}
void solve() {
  cin >> n;
  reset();
  for (int i = 0; i < n; ++i) {
    int m, c;
    cin >> m >> c;
    a[m].push_back(c);
  }
  long long sum = 0;
  int used = 0;
  int smartNum = n;
  for (int i = n - 1; i >= 0; --i) {
    for (int x : a[i]) pq.push(x);
    smartNum -= (int)a[i].size();
    while (smartNum + used < i && !pq.empty()) {
      sum += pq.top();
      pq.pop();
      ++used;
    }
  }
  cout << sum << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  cin >> t;
  while (t--) {
    solve();
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
