#include <bits/stdc++.h>
using namespace std;
struct node {
  long long m, p, id;
};
bool cmpM(node a, node b) {
  if (a.m != b.m) return a.m < b.m;
  return a.p > b.p;
}
long long T, N, INF, ans, rnd;
node a[200005];
long long vis[200005];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> T;
  while (T--) {
    cin >> N;
    INF = 0;
    long long buy = N;
    for (long long i = 1; i <= N; i++) {
      cin >> a[i].m >> a[i].p;
      INF += a[i].p;
      a[i].id = i;
      buy = min(buy, a[i].m);
    }
    sort(a + 1, a + N + 1, cmpM);
    ans = INF;
    for (long long i = 1; i + buy - 1 <= N; i++) {
      long long tmp = 0;
      long long cnt = 0;
      for (long long j = i; j <= i + buy - 1; j++) {
        tmp += a[j].p;
        cnt++;
      }
      for (long long j = 1; j <= N; j++) {
        if (j >= i && j <= i + buy - 1) continue;
        if (cnt >= a[j].m) cnt++;
      }
      if (cnt == N) ans = min(ans, tmp);
    }
    cout << ans << "\n";
  }
  return 0;
}
