#include <bits/stdc++.h>
using namespace std;
int T;
int n;
priority_queue<int, vector<int>, greater<int> > pq;
vector<int> v[200005];
int main() {
  ios::sync_with_stdio(false);
  cin >> T;
  while (T--) {
    cin >> n;
    long long ans = 0;
    for (int i = 1; i <= n; ++i) v[i].clear();
    for (int i = 1; i <= n; ++i) {
      int mi, pi;
      cin >> mi >> pi;
      v[mi].push_back(pi);
    }
    int sum = n, cnt = 0;
    for (int i = n - 1; i >= 0; --i) {
      if (v[i].empty()) continue;
      sum -= v[i].size();
      for (int j = 0; j < v[i].size(); ++j) pq.push(v[i][j]);
      while (cnt < i - sum) {
        ans += pq.top();
        pq.pop();
        ++cnt;
      }
    }
    while (!pq.empty()) pq.pop();
    cout << ans << endl;
  }
  return 0;
}
