#include <bits/stdc++.h>
using namespace std;
vector<int> g[200011];
priority_queue<int, vector<int>, greater<int> > q;
int main() {
  int t, i;
  scanf("%d", &t);
  while (t > 0) {
    int n;
    while (!q.empty()) q.pop();
    scanf("%d", &n);
    for (i = 0; i <= n; i++) g[i].clear();
    for (i = 0; i < n; i++) {
      int mi, pi;
      scanf("%d%d", &mi, &pi);
      g[mi].push_back(pi);
    }
    int tol = n, cnt = 0;
    long long ans = 0;
    for (i = n - 1; i >= 0; i--) {
      tol -= g[i].size();
      int req = i - tol;
      for (auto v : g[i]) q.push(v);
      while (cnt < req) {
        cnt++;
        ans += q.top();
        q.pop();
      }
    }
    printf("%lld\n", ans);
    t--;
  }
  return 0;
}
