#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
vector<int> v[N];
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 = 0; i < n; ++i) {
      int mi, pi;
      scanf("%d%d", &mi, &pi);
      v[mi].push_back(pi);
    }
    priority_queue<int, vector<int>, greater<int>> q;
    long long ans = 0;
    for (int i = n - 1; i >= 0; --i) {
      for (const auto &j : v[i]) {
        q.push(j);
      }
      while (q.size() > n - i) {
        ans += q.top();
        q.pop();
      }
    }
    while (!q.empty()) {
      q.pop();
    }
    printf("%lld\n", ans);
  }
  return 0;
}
