#include <bits/stdc++.h>
using namespace std;
long long ans = 0, tp, m, p, t, _, n, i, j, sum[200005], h;
priority_queue<long long, vector<long long>, greater<long long> > pq;
vector<long long> s[200005];
int main() {
  scanf("%lld", &t);
  for (_ = 1; _ <= t; _++) {
    scanf("%lld", &n);
    for (i = 0; i <= n; i++) sum[i] = 0;
    while (!pq.empty()) pq.pop();
    ans = tp = 0;
    for (i = 0; i <= n; i++) s[i].clear();
    for (i = 1; i <= n; i++) {
      scanf("%lld%lld", &m, &p);
      s[m].push_back(p);
      sum[m]++;
    }
    for (i = 1; i <= n; i++) sum[i] += sum[i - 1];
    for (i = n - 1; i >= 0; i--) {
      while (tp + sum[i] <= i) {
        h = pq.top();
        ans += h;
        tp++;
        pq.pop();
      }
      for (j = 0; j < s[i].size(); j++) pq.push(s[i][j]);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
