#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
void read(int &a) {
  a = 0;
  int d = 1;
  char ch;
  while (ch = getchar(), ch > '9' || ch < '0')
    if (ch == '-') d = -1;
  a = ch ^ 48;
  while (ch = getchar(), ch >= '0' && ch <= '9')
    a = (a << 3) + (a << 1) + (ch ^ 48);
  a *= d;
}
vector<int> v[N];
priority_queue<int, vector<int>, greater<int> > q;
int main() {
  int T;
  read(T);
  while (T--) {
    while (!q.empty()) q.pop();
    int n;
    read(n);
    for (register int i = 0; i <= n; i++) v[i].clear();
    for (register int i = 1; i <= n; i++) {
      int m, p;
      read(m), read(p);
      v[m].push_back(p);
    }
    long long ans = 0;
    for (register int i = n; i >= 0; i--) {
      for (register int j = 0; j < v[i].size(); j++) q.push(v[i][j]);
      while (q.size() > n - i) {
        ans += q.top();
        q.pop();
      }
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
