#include <bits/stdc++.h>
using namespace std;
long long t, n;
vector<long long> v[200005];
multiset<long long> s;
signed main() {
  for (scanf("%lld", &t); t--;) {
    scanf("%lld", &n), s.clear();
    long long lst = n, cnt = 0, res = 0;
    for (long long i = 0; i <= n; i++) v[i].clear();
    for (long long i = 1, m, p; i <= n; i++)
      scanf("%lld%lld", &m, &p), v[m].push_back(p);
    for (long long i = n, t; i >= 0; i--) {
      lst -= v[i].size(), t = i - lst - cnt;
      for (long long j = 0; j < (long long)v[i].size(); j++) s.insert(v[i][j]);
      for (; t >= 1; t--) cnt++, res += *s.begin(), s.erase(*s.begin());
    }
    printf("%lld\n", res);
  }
  return 0;
}
