#include <bits/stdc++.h>
using namespace std;
pair<int, long long> a[200005];
vector<int> check[200005];
pair<int, int> b[400005];
int s[400005];
void process() {
  int n;
  cin >> n;
  int i, j;
  for (i = 0; i < n; i++) check[i].clear();
  for (i = 1; i <= n; i++) {
    cin >> a[i].first >> a[i].second;
    check[a[i].first].push_back(a[i].second);
  }
  int cnt = 0;
  for (i = 0; i <= n; i++) {
    if (i) b[++cnt] = {-1, 0};
    for (j = 0; j < check[i].size(); j++) {
      b[++cnt] = {1, check[i][j]};
    }
  }
  int t = 0;
  long long ans = 0;
  for (i = 1; i <= 2 * n; i++) s[i] = s[i - 1] + b[i].first;
  priority_queue<int, vector<int>, greater<int>> Q;
  for (i = 2 * n; i >= 1; i--) {
    if (b[i].first == 1) {
      Q.push(b[i].second);
    }
    if (s[i] + t < 0) {
      t++;
      ans += Q.top();
      Q.pop();
    }
  }
  cout << ans << "\n";
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int t;
  cin >> t;
  for (int j = 0; j < t; j++) {
    process();
  }
  return 0;
}
