#include <bits/stdc++.h>
using namespace std;
const long long pp = 1e9 + 7;
const long double eps = 1e-8;
inline long long ksm(long long x, long long y = pp - 2) {
  x = (x % pp + pp) % pp;
  long long ans = 1;
  while (y) {
    if (y & 1) ans = ans * x % pp;
    x = x * x % pp;
    y >>= 1;
  }
  return ans;
}
inline long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
inline void reads(char s[], int &n) {
  n = 0;
  char ch = getchar();
  while (ch == ' ' || ch == '\n' || ch == '\r') ch = getchar();
  while (ch != ' ' && ch != '\n' && ch != '\r') s[++n] = ch, ch = getchar();
}
int n;
struct VOTER {
  long long p;
  int m;
} a[200009];
inline bool cmp(const VOTER &a, const VOTER &b) { return a.m < b.m; }
long long f[5009][5009];
int main() {
  int Tt;
  scanf("%d", &Tt);
  while (Tt--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i].m);
      scanf("%I64d", &a[i].p);
    }
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; i++)
      for (int j = 0; j <= n - i; j++) {
        f[i][j] = f[i - 1][j + 1] + a[i].p;
        if (i - 1 + j >= a[i].m) f[i][j] = min(f[i][j], f[i - 1][j]);
      }
    printf("%I64d\n", f[n][0]);
  }
  return 0;
}
