#include <bits/stdc++.h>
using namespace std;
long long t, n;
multiset<long long> a[200010];
long long m[200010];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    for (long long i = (0), i__end = (n); i < i__end; i++) {
      long long m, p;
      cin >> m >> p;
      a[m].insert(p);
    }
    long long cnt = 0;
    for (long long i = (0), i__end = (n); i < i__end; i++) {
      if (a[i].empty())
        m[i] = 0, cnt++;
      else {
        m[i] = *a[i].rend();
        a[i + 1].insert(a[i].begin(), prev(a[i].end()));
        a[i] = multiset<long long>();
      }
    }
    long long ans = 0;
    for (long long i = (0), i__end = (min(cnt, (long long)a[n].size()));
         i < i__end; i++) {
      ans += *a[n].begin();
      a[n].erase(*a[n].begin());
    }
    a[n] = multiset<long long>();
    cout << ans << endl;
  }
  return 0;
}
