#include <bits/stdc++.h>
using namespace std;
long long n, m, k;
long long read() {
  long long s = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * f;
}
pair<long long, long long> p[202020];
signed main() {
  long long T = read();
  while (T--) {
    n = read();
    for (long long i = 0, iend = n - 1; i <= iend; ++i)
      p[i].first = read(), p[i].second = read();
    sort(p, p + n);
    p[n].first = 1010101019;
    priority_queue<long long> q;
    long long nw = 0;
    for (long long i = 0, iend = n - 1; i <= iend; ++i) {
      while (p[nw].first == i) q.push(p[nw++].second);
      if (q.size()) q.pop();
    }
    long long ans = 0;
    while (!q.empty()) {
      ans += q.top();
      q.pop();
    }
    cout << ans << endl;
  }
}
