#include <bits/stdc++.h>
using namespace std;
namespace jumpmelon {
const int MAXN = 5000;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
int C[MAXN + 1];
long long F[MAXN + 2][MAXN + 1];
struct voter {
  int m, p;
} A[MAXN + 1];
inline bool operator<(const voter &a, const voter &b) {
  return a.m != b.m ? a.m < b.m : a.p > b.p;
}
inline void to_min(long long &a, long long b) {
  if (b < a) a = b;
}
void work() {
  int kase;
  scanf("%d", &kase);
  while (kase--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d%d", &A[i].m, &A[i].p);
    memset(C + 1, 0, sizeof(int[n]));
    for (int i = 1; i <= n; i++) C[A[i].m + 1]++;
    for (int i = 1; i <= n; i++) C[i] += C[i - 1];
    sort(A + 1, A + n + 1);
    F[n + 1][0] = 0;
    for (int i = n; i >= 1; i--) {
      for (int j = n - i + 1; j >= 0; j--) F[i][j] = INF;
      for (int j = n - i + 1; j >= 1; j--)
        to_min(F[i][j], F[i + 1][j - 1] + A[i].p);
      for (int j = n - i; j >= 0 && j >= A[i].m - C[A[i].m]; j--)
        to_min(F[i][j], F[i + 1][j]);
    }
    long long ans = INF;
    for (int i = 0; i <= n; i++) ans = min(ans, F[1][i]);
    printf("%lld\n", ans);
  }
}
}  // namespace jumpmelon
int main() {
  jumpmelon::work();
  return 0;
}
