#include <bits/stdc++.h>
using namespace std;
vector<int> vec[200010];
int cnt[200010];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      int m, p;
      scanf("%d %d", &m, &p);
      vec[m].push_back(p);
      cnt[m]++;
    }
    for (int i = 1; i < n; i++) cnt[i] += cnt[i - 1];
    priority_queue<int, vector<int>, greater<int> > q;
    long long ans = 0;
    int now = 0;
    for (int i = n - 1; i >= 1; i--) {
      for (int j = 0; j < vec[i].size(); j++) q.push(vec[i][j]);
      while (now + cnt[i - 1] < i) {
        ans += q.top();
        q.pop();
        now++;
      }
    }
    printf("%I64d\n", ans);
    for (int i = 0; i < n; i++) cnt[i] = 0, vec[i].clear();
  }
  return 0;
}
