#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;
int n;
vector<int> vec[MAXN];
priority_queue<int, vector<int>, greater<int> > q;
void work() {
  scanf("%d", &n);
  for (register int i = 0; i <= n; ++i) vec[i].clear();
  for (register int i = 1; i <= n; ++i) {
    int c, t;
    scanf("%d%d", &c, &t);
    vec[c].push_back(t);
  }
  long long ans = 0;
  while (!q.empty()) q.pop();
  for (register int i = n; i >= 1; i--) {
    for (register int j = 0; j < (int)vec[i].size(); ++j) q.push(vec[i][j]);
    while (n - i < (int)q.size()) ans += q.top(), q.pop();
  }
  printf("%lld\n", ans);
}
int main() {
  int _ = 1;
  scanf("%d", &_);
  while (_--) work();
  return 0;
}
