#include <bits/stdc++.h>
using namespace std;
const int tam = 100010;
const int MOD = 1e9 + 7;
const int MOD1 = 988244535;
const double EPS = 1e-9;
const double PI = acos(-1);
vector<pair<long long, int>> tips[200020];
pair<int, pair<long long, int>> ar[200020];
bool used[200020];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int cas;
  cin >> cas;
  long long res;
  while (cas--) {
    res = 0;
    int us = 0;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) used[i] = 0;
    for (int i = 0; i < n; i++) {
      cin >> ar[i].first >> ar[i].second.first;
      ar[i].second.second = i;
    }
    priority_queue<int, vector<int>, greater<int>> que;
    sort(ar, ar + n);
    for (int i = n - 1; i > -1; i--) {
      que.push(ar[i].second.first);
      if (ar[i].first > us + i) {
        us++;
        res += que.top();
        que.pop();
      }
    }
    cout << res << '\n';
    for (int i = 1; i < n; i++) tips[i].clear();
  }
  return 0;
}
