#include <bits/stdc++.h>
using namespace std;
const int maxn = 233333;
int n;
vector<int> v[maxn];
int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> n;
    for (int i = 0; i < n; i++) v[i].clear();
    for (int i = 1; i <= n; i++) {
      int m, p;
      cin >> m >> p;
      v[m].push_back(p);
    }
    priority_queue<long long, vector<long long>, greater<long long> > q;
    long long pre = n;
    int cnt = 0;
    long long ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      pre -= v[i].size();
      for (int j = 0; j < v[i].size(); j++) {
        q.push(v[i][j]);
      }
      while (cnt + pre < i) {
        ans += q.top();
        q.pop();
        cnt++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
