#include <bits/stdc++.h>
const int N = 2e5 + 100;
const double EPS = 1e-9;
using namespace std;
void dbg() { cout << "\n"; }
template <typename T, typename... A>
void dbg(T a, A... x) {
  cout << a << ' ';
  dbg(x...);
}
int n, m, p;
vector<int> v[N];
priority_queue<int> q;
int main() {
  int _;
  scanf("%d", &_);
  while (_--) {
    scanf("%d", &n);
    for (int i = 0; i <= n; i++) v[i].clear();
    for (int i = 1; i <= n; i++) scanf("%d%d", &m, &p), v[m].push_back(p);
    while (q.size()) q.pop();
    long long ans = 0;
    for (int i = n; i >= 0; i--) {
      for (auto& tp : v[i]) q.push(-tp);
      while (q.size() > n - i) ans -= q.top(), q.pop();
    }
    printf("%lld\n", ans);
  }
  return 0;
}
