#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
int arr[MAX];
int n;
vector<int> v[MAX];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; ++i) v[i].clear();
    for (int i = 0; i < n; ++i) {
      int x, y;
      cin >> x >> y;
      v[x].push_back(y);
    }
    long long ans = 0;
    priority_queue<int, vector<int>, greater<int> > q;
    for (int i = n - 1; i >= 0; --i) {
      for (auto &j : v[i]) q.push(j);
      while (q.size() > n - i) ans += q.top(), q.pop();
    }
    cout << ans << "\n";
  }
  return 0;
}
