#include <bits/stdc++.h>
using namespace std;
int long long t, n, i, l, r, m, k, cnt, ans;
multiset<int long long> me, my[200005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> t;
  while (t) {
    t--;
    cin >> n;
    for (i = 1; i <= n; i++) {
      int x, q;
      cin >> x >> q;
      my[x].insert(q);
    }
    cnt = my[0].size();
    my[0].clear();
    ans = 0;
    l = 0;
    while (l <= cnt) {
      cnt += my[l].size();
      my[l].clear();
      l++;
    }
    for (i = n; i >= l; i--) {
      int x = my[i].size();
      while (!my[i].empty()) {
        me.insert(*my[i].begin());
        my[i].erase(my[i].begin());
      }
      while (me.size() > (n - i)) {
        ans += *me.begin();
        cnt++;
        while (l <= cnt) {
          cnt += my[l].size();
          my[l].clear();
          l++;
        }
        me.erase(*me.begin());
      }
    }
    cout << ans << endl;
    me.clear();
  }
}
