#include <bits/stdc++.h>
using namespace std;
int t, n, tot;
vector<int> w[200005];
multiset<int> mts;
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    mts.clear();
    for (int i = 0; i <= n; i++) w[i].clear();
    for (int i = 1, x, y; i <= n; i++) {
      cin >> x >> y;
      w[x].push_back(y);
    }
    tot = 0;
    for (int i = 0; i <= n; i++) tot += w[i].size();
    int cur = 0;
    long long ans = 0;
    for (int i = n; i >= 0; i--) {
      tot -= w[i].size();
      for (int j = 0; j < w[i].size(); j++) mts.insert(w[i][j]);
      int need = i - tot;
      while (cur < need) {
        ans += *mts.begin();
        mts.erase(mts.begin());
        cur++;
      }
    }
    cout << ans << '\n';
  }
}
