#include <bits/stdc++.h>
using namespace std;
long long n, t, a[5001], b[5001], dp[5001][5001], c[5001];
inline bool cmp(long long x, long long y) { return a[x] < a[y]; }
inline long long read() {
  long long sum = 0, x = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') x = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    sum = sum * 10 + ch - '0';
    ch = getchar();
  }
  return sum * x;
}
inline void write(long long x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
  return;
}
signed main() {
  t = read();
  while (t--) {
    n = read();
    for (register long long i = 1; i <= n; ++i) {
      a[i] = read();
      b[i] = read();
      c[i] = i;
    }
    for (register long long i = 1; i <= n; ++i)
      for (register long long j = 0; j <= n; ++j) dp[i][j] = 1e17;
    sort(c + 1, c + n + 1, cmp);
    for (register long long i = 0; i < n; ++i)
      for (register long long j = 0; j <= n; ++j) {
        dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i][j] + b[c[i + 1]]);
        if (i + j >= a[c[i + 1]]) dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      }
    write(dp[n][0]);
    putchar('\n');
  }
  return 0;
}
