#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
struct my_compare {
  bool operator()(pair<int, pair<int, int> > &lhs,
                  pair<int, pair<int, int> > &rhs) const {
    return (lhs.second.first < rhs.second.first) ||
           (lhs.second.first == rhs.second.first &&
            lhs.second.second > rhs.second.second);
  }
};
int main() {
  long long int t, n, q;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<pair<int, pair<int, int> > > A(n);
    for (int i = 0; i < n; i++) {
      A[i].first = i + 1;
      cin >> A[i].second.first >> A[i].second.second;
    }
    sort(A.begin(), A.end(), my_compare());
    vector<long long int> b(n, 0);
    long long int ourside = 0;
    long long int ans = 0;
    for (int i = 0; i < n; i++) {
      if (b[i] == 1) continue;
      while (ourside < A[i].second.first) {
        long long int mini = 1e9 + 1, ind = n + 1;
        for (int j = i + 1; j < n; j++) {
          if (b[j] == 0 && A[j].second.second < mini) {
            mini = A[j].second.second;
            ind = j;
          }
        }
        ans += A[ind].second.second;
        if (ind == i) break;
        b[ind] = 1;
        ourside++;
      }
      b[i] = 1;
      ourside++;
    }
    cout << ans << endl;
  }
  return 0;
}
