#include <bits/stdc++.h>
using namespace std;
const long long oo = 1e15 + 7;
const long long mod = 1e9 + 7;
const int N = 3e5 + 7;
int test, n;
vector<int> v[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> test;
  while (test--) {
    cin >> n;
    for (int i = 0; i <= n; i++) v[i].clear();
    int m, x;
    for (int i = 0; i <= n - 1; i++) {
      cin >> m >> x;
      v[m].push_back(x);
    }
    multiset<int> q;
    long long res = 0;
    int pref = n;
    int cnt = 0;
    for (int i = n - 1; i >= 0; i--) {
      pref -= v[i].size();
      int need = i - pref;
      for (auto x : v[i]) q.insert(x);
      while (cnt < need) {
        cnt++;
        res += *q.begin();
        q.erase(q.begin());
      }
    }
    cout << res << "\n";
  }
  return 0;
}
