#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int n;
vector<long long> v[1 + N];
void solve() {
  cin >> n;
  for (int i = 0; i <= n; i++) v[i].clear();
  for (int i = 1; i <= n; i++) {
    int m;
    long long p;
    cin >> m >> p;
    v[m].push_back(p);
  }
  long long ans = 0;
  long long cur = 0;
  priority_queue<long long> pq;
  for (int i = n; i >= 0; i--) {
    for (auto x : v[i]) pq.push(-x);
    cur += v[i].size();
    while (i + cur > n) {
      ans += -pq.top();
      pq.pop();
      cur--;
    }
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
