#include <bits/stdc++.h>
using namespace std;
int n;
const int N = 2e5 + 3;
vector<long long> g[N];
priority_queue<long long, vector<long long>, greater<long long> > q;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 0; i <= n; i++) g[i].clear();
    int x;
    long long y, ans = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d%lld", &x, &y);
      g[x].push_back(y);
    }
    while (!q.empty()) q.pop();
    for (int i = n - 1; i >= 0; i--) {
      int sz = g[i].size();
      for (int j = 0; j < sz; j++) q.push(g[i][j]);
      int mn = n - i;
      sz = q.size();
      while (sz-- > mn) {
        ans += q.top();
        q.pop();
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
