#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long int const mod = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t, n, x, m, p, pf, sum, cnt;
  cin >> t;
  while (t--) {
    cin >> n;
    sum = 0;
    multiset<long long int> ss;
    vector<long long int> v1[n + 2];
    cnt = 0;
    for (long long int i = 0; i < n; i++) {
      cin >> m >> p;
      v1[m].push_back(p);
    }
    pf = n;
    for (long long int i = n - 1; i >= 0; i--) {
      pf -= v1[i].size();
      x = i - pf;
      for (auto it = v1[i].begin(); it != v1[i].end(); it++) ss.insert(*it);
      while (cnt < x) {
        cnt += 1;
        sum += *(ss.begin());
        ss.erase(ss.begin());
      }
    }
    cout << sum << "\n";
  }
  return 0;
}
