#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
vector<int> v[maxn];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 0; i <= n; i++) v[i].clear();
    for (int i = 1; i <= n; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      v[a].push_back(b);
    }
    priority_queue<int, vector<int>, greater<int> > q;
    long long ans = 0;
    for (int i = n; i >= 1; i--) {
      for (int j = 0; j < v[i].size(); j++) q.push(v[i][j]);
      while (q.size() > n - i) ans += q.top(), q.pop();
    }
    cout << ans << endl;
  }
  return 0;
}
