#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y, sum, cnt, ans, q;
vector<int> v[200005];
int buy[200005];
int freq[200005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    for (int i = (0); i <= (n); i++) {
      buy[i] = freq[i] = 0;
      v[i].clear();
    }
    for (int i = (1); i <= (n); i++) {
      scanf("%d %d", &x, &y);
      freq[x]++;
      v[x].push_back(y);
    }
    int cur = 0;
    for (int i = (0); i <= (n - 1); i++) {
      if (cur < i) buy[i] = i - cur;
      cur += freq[i];
    }
    priority_queue<int> pq;
    int curmax = 0;
    long long cost = 0;
    for (int i = (n - 1); i >= (0); i--) {
      for (auto it : v[i]) {
        pq.push(-it);
      }
      for (int j = (1); j <= (buy[i] - curmax); j++) {
        cost -= pq.top();
        pq.pop();
      }
      curmax = max(curmax, buy[i]);
    }
    printf("%lld\n", cost);
  }
  return 0;
}
