#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
const long long inf = 2e18;
const int infi = 15e8;
long long n, m, k, qq, fr, to, w, mod = 1e9 + 7;
char ch;
void solve() {
  cin >> n;
  vector<vector<long long>> d(n);
  for (long long i = 0; i < n; i++) {
    long long pos, x;
    cin >> pos >> x;
    d[pos].push_back(x);
  }
  long long sum = n, cnt = 0, ans = 0;
  multiset<long long> second;
  for (long long i = n - 1; i >= 0; i--) {
    sum -= d[i].size();
    long long need = i - sum;
    for (auto x : d[i]) second.insert(x);
    while (cnt < need) {
      long long x = *second.begin();
      second.erase(second.begin());
      cnt++;
      ans += x;
    }
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> qq;
  while (qq--) {
    solve();
  }
}
