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