#include <bits/stdc++.h>
using namespace std;
long long T, n;
long long f[2100][2100];
struct node {
  long long m, p, num;
} a[1100000];
long long cmp(node a, node b) { return a.m < b.m; }
signed main() {
  cin >> T;
  while (T--) {
    cin >> n;
    for (long long i = 1; i <= n; i++)
      scanf("%d%d", &a[i].m, &a[i].p), a[i].num = i;
    for (long long i = 1; i <= n; i++)
      for (long long j = 0; j <= n; j++) f[i][j] = 2e9;
    sort(a + 1, a + n + 1, cmp);
    for (long long i = 0; i < n; i++)
      for (long long j = 0; j <= n; j++) {
        f[i + 1][j - 1] = min(f[i + 1][j - 1], f[i][j] + a[a[i + 1].num].p);
        if (i + j >= a[a[i + 1].num].m) f[i + 1][j] = min(f[i + 1][j], f[i][j]);
      }
    cout << f[n][0] << endl;
  }
  return 0;
}
