#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    vector<int> v[5005];
    long long sum = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      int x, y;
      cin >> x >> y;
      v[x].push_back(y);
    }
    priority_queue<int> q;
    for (int i = n; i >= 0; i--) {
      for (int j = 0; j < v[i].size(); j++) {
        q.push(-v[i][j]);
        while (q.size() > n - i) {
          sum -= q.top();
          q.pop();
        }
      }
    }
    cout << sum << endl;
  }
}
