#include <bits/stdc++.h>
using namespace std;
const long long INF = 2000000000000000000LL;
const int inf = 0x3f3f3f3f;
const long double EPS = 1e-9;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int t, tc = 0;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<int> > prices(n);
    for (int i = 0; i < n; ++i) {
      int m, p;
      cin >> m >> p;
      prices[m].push_back(p);
    }
    priority_queue<int, vector<int>, greater<int> > pq;
    long long res = 0;
    for (int i = n - 1; i >= 0; --i) {
      for (auto &qq : prices[i]) {
        pq.push(qq);
      }
      while ((int)pq.size() > n - i) {
        res += pq.top();
        pq.pop();
      }
    }
    cout << res << "\n";
  }
  return 0;
}
