#include <bits/stdc++.h>
using namespace std;
vector<long long> maps[200009];
int main() {
  long long t, i, j, k, n, m, x;
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld", &n);
    for (i = 0; i <= n; i++) maps[i].clear();
    for (i = 0; i <= n; i++) {
      scanf("%lld", &j);
      scanf("%lld", &k);
      maps[j].push_back(k);
    }
    x = 0;
    priority_queue<long long, vector<long long>, greater<long long> > q;
    for (i = n; i >= 0; i--) {
      long long len = maps[i].size();
      for (j = 0; j < len; j++) q.push(maps[i][j]);
      while (q.size() > n - i) {
        x += q.top();
        q.pop();
      }
    }
    printf("%lld\n", x);
  }
  return 0;
}
