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