#include <bits/stdc++.h>
using namespace std;
long long n;
vector<long long> pe[200003];
int main() {
  ios::sync_with_stdio(0);
  int t = 0;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      int x, y;
      cin >> x >> y;
      pe[x].push_back(y);
    }
    long long js = n;
    long long ans = 0;
    long long tt = 0;
    multiset<long long> s;
    for (int i = n - 1; i >= 1; i--) {
      n -= pe[i].size();
      long long need = i - n - tt;
      for (auto k : pe[i]) s.insert(k);
      if (need > 0) {
        for (int u = 1; u <= need; u++)
          ans += *s.begin(), s.erase(s.begin()), tt++;
      }
    }
    cout << ans << endl;
    for (int i = 1; i <= n; i++) pe[i].clear();
  }
  return 0;
}
