#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<long long>> m(n);
    for (int i = 0; i < n; i++) {
      long long x, y;
      cin >> x >> y;
      m[x].push_back(y);
    }
    vector<int> p(n);
    for (int i = 1; i < n; i++) p[i] = m[i - 1].size() + p[i - 1];
    int cant = 0;
    long long ans = 0;
    multiset<long long> s;
    for (int i = n - 1; i >= 0; i--) {
      for (auto &j : m[i]) s.insert(j);
      if (p[i] + cant >= i) continue;
      while (cant < i - p[i]) {
        ++cant;
        auto it = s.begin();
        ans += *it;
        s.erase(it);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
