#include <bits/stdc++.h>
using namespace std;
int n;
int num[200003];
vector<int> vec[200003];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 0; i <= n; i++) vec[i].clear();
    for (int i = 1; i <= n; i++) {
      int x, cost;
      scanf("%d %d", &x, &cost);
      vec[x].push_back(cost);
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
      if (sum >= i)
        num[i] = 0;
      else
        num[i] = i - sum;
      sum += vec[i].size();
    }
    int mx = 0;
    long long res = 0;
    priority_queue<int, vector<int>, greater<int> > pq;
    for (int i = n - 1; i >= 0; i--) {
      for (int x : vec[i]) pq.push(x);
      while (mx < num[i]) {
        mx++;
        res += pq.top();
        pq.pop();
      }
    }
    printf("%lld\n", res);
  }
}
