#include <bits/stdc++.h>
using namespace std;
long long n;
pair<long long, long long> a[200005], b[200005];
bool cmp(pair<long long, long long> lp, pair<long long, long long> rp) {
  if (lp.first != rp.first)
    return lp.first > rp.first;
  else
    return lp.second < rp.second;
}
bool cmp2(pair<long long, long long> lp, pair<long long, long long> rp) {
  if (lp.first != rp.first)
    return lp.first < rp.first;
  else
    return lp.second < rp.second;
}
int main(int argc, char const *argv[]) {
  long long t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> a[i].first >> a[i].second;
      b[i] = a[i];
    }
    sort(a, a + n, cmp);
    sort(b, b + n, cmp2);
    long long ans = 0, sz = 0;
    for (int i = 0; i < n; ++i) {
      long long cnt = sz, size = sz;
      for (int j = 0; j < n; ++j) {
        if (b[j].first <= size) {
          size++;
          cnt++;
        }
      }
      if (cnt < a[i].first) {
        ans += a[i].second;
        sz++;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
