#include <bits/stdc++.h>
using namespace std;
int read() {
  int r = 0, t = 1, c = getchar();
  while (c < '0' || c > '9') {
    t = c == '-' ? -1 : 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = (r << 3) + (r << 1) + (c ^ 48);
    c = getchar();
  }
  return r * t;
}
const int N = 200010;
int n, t, s;
long long ans;
priority_queue<int> q;
pair<int, int> a[N];
int main() {
  t = read();
  while (t--) {
    n = read();
    for (int i = (1), end_i = (n); i <= end_i; i++) {
      a[i].first = read();
      a[i].second = read();
    }
    ans = s = 0;
    sort(a + 1, a + n + 1);
    while (!q.empty()) q.pop();
    for (int i = n, j; i; i = j - 1) {
      j = i;
      while (j > 1 && a[j - 1].first == a[i].first) j--;
      for (int k = (j), end_k = (i); k <= end_k; k++) q.push(-a[k].second);
      while (s < a[i].first - (j - 1)) {
        s++;
        ans += -q.top();
        q.pop();
      }
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
