#include <bits/stdc++.h>
using namespace std;
int T, n;
vector<int> vec[200010];
priority_queue<int, vector<int>, greater<int> > pq;
bool cmp(int x, int y) { return x > y; }
void solve() {
  while (!pq.empty()) pq.pop();
  scanf("%d", &n);
  for (int i = 0; i <= n; i++) vec[i].clear();
  for (int i = 1, m, p; i <= n; i++) {
    scanf("%d%d", &m, &p);
    vec[m].push_back(p);
  }
  long long ans = 0;
  for (int i = n; i >= 0; i--) {
    for (int j = 0; j < vec[i].size(); j++) pq.push(vec[i][j]);
    while (pq.size() > n - i) ans += pq.top(), pq.pop();
  }
  printf("%lld\n", ans);
}
int main() {
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}
