#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 200000;
int t;
int n;
int m[N_MAX + 2], p[N_MAX + 2];
int pref[N_MAX + 2];
vector<int> vp[N_MAX + 2];
multiset<int> s;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> m[i] >> p[i];
    for (int i = 0; i < n; i++) pref[i] = 0;
    for (int i = 1; i <= n; i++) pref[m[i]]++;
    for (int i = 1; i < n; i++) pref[i] += pref[i - 1];
    for (int i = 1; i <= n; i++) vp[m[i]].push_back(p[i]);
    int cnt = 0;
    long long answer = 0;
    for (int i = n - 1; i >= 1; i--) {
      for (int x : vp[i]) s.insert(x);
      while (pref[i - 1] + cnt < i) {
        answer += *s.begin();
        s.erase(s.begin());
        cnt++;
      }
    }
    cout << answer << "\n";
    s.clear();
    for (int i = 0; i < n; i++) vp[i].clear();
  }
  return 0;
}
