#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n;
vector<int> vi[maxn];
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) vi[i].clear();
    for (int i = 1, m, p; i <= n; i++) {
      cin >> m >> p;
      vi[m].push_back(p);
    }
    priority_queue<int, vector<int>, greater<int> > Q;
    long long ans = 0;
    for (int i = n; i >= 1; i--) {
      int len = (int)vi[i].size();
      for (int j = 0; j < len; j++) Q.push(vi[i][j]);
      while (Q.size() > n - i) {
        ans += Q.top();
        Q.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
