#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<pair<int, int>> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second;
  long long ans = 1000000000000000000;
  for (int i = 0; i < n; ++i) {
    sort(a.begin(), a.end(), [](pair<int, int> &a, pair<int, int> &b) {
      return a.first < b.first || a.first == b.first && a.second > b.second;
    });
    int cnt = 0;
    for (int j = 0; j <= i; ++j) cnt += (a[j].first - (n - i - 1) > j);
    long long sum = 0;
    for (int j = i + 1; j < n; ++j) sum += a[j].second;
    sort(a.begin(), a.begin() + i + 1,
         [](pair<int, int> &a, pair<int, int> &b) {
           return a.second < b.second;
         });
    for (int j = 0; j < cnt; ++j) sum += a[j].second;
    ans = min(ans, sum);
  }
  cout << ans << endl;
}
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; ++i) solve();
  return 0;
}
