#include <bits/stdc++.h>
using namespace std;
vector<long long> groups[200005];
long long n, t, ans;
void clean() {
  for (long long i = 0; i <= n; i++) groups[i].clear();
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> t;
  while (t--) {
    ans = 0;
    cin >> n;
    clean();
    for (long long i = 0; i < n; i++) {
      long long m, p;
      cin >> m >> p;
      groups[m].push_back(p);
    }
    priority_queue<long long> pq;
    for (long long i = n; i >= 0; i--) {
      for (long long j = 0; j < groups[i].size(); j++) pq.push(-groups[i][j]);
      while (pq.size() > n - i) {
        ans -= pq.top();
        pq.pop();
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
