#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int N;
    scanf("%d", &N);
    vector<tuple<int, int, int> > dat(N), tmp;
    bool done[N];
    for (int i = 0; i < N; ++i)
      scanf("%d", &get<0>(dat[i])), scanf("%d", &get<1>(dat[i])),
          get<2>(dat[i]) = i;
    tmp = dat;
    sort((dat).begin(), (dat).end());
    sort((tmp).begin(), (tmp).end(),
         [](const tuple<int, int, int>& A, const tuple<int, int, int>& B) {
           return (pair<int, pair<int, int> >){get<1>(A),
                                               {get<0>(A), get<2>(A)}} <
                  (pair<int, pair<int, int> >){get<1>(B),
                                               {get<0>(B), get<2>(B)}};
         });
    long long ans = 1e15;
    for (int i = -1; i < N; ++i) {
      memset((done), 0, sizeof(done));
      long long curr = i >= 0 ? get<1>(dat[i]) : 0;
      if (i >= 0) done[get<2>(dat[i])] = 1;
      int cnt = i >= 0, k = 0;
      for (int j = 0; j < N; ++j) {
        if (done[get<2>(dat[j])])
          continue;
        else if (!done[get<2>(dat[j])] && cnt >= get<0>(dat[j])) {
          ++cnt;
          done[get<2>(dat[j])] = 1;
        } else {
          for (; k < N;) {
            if (!done[get<2>(tmp[k])]) {
              ++cnt;
              done[get<2>(tmp[k])] = 1;
              curr += get<1>(tmp[k]);
              --j;
              ++k;
              break;
            } else {
              ++k;
            }
          }
        }
      }
      ans = min(ans, curr);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
