#include <bits/stdc++.h>
inline int read() {
  char c;
  int x;
  for (c = getchar(); !isdigit(c); c = getchar())
    ;
  for (x = 0; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  return x;
}
const int N = 3 + 5;
int T, n;
long long f[N][N];
std::pair<int, int> a[N];
int main() {
  for (int T = read(); T; T--) {
    n = read();
    for (int i = 0; i < n; i++) {
      a[i].first = read();
      a[i].second = read();
    }
    std::sort(a, a + n);
    f[0][0] = 0;
    for (int i = 1; i <= n; i++) {
      f[0][i] = 1e18;
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j <= n; j++) {
        f[i][j] = std::min(f[i][j], f[i][j - 1]);
      }
      for (int j = i + 1; j <= n; j++) {
        f[i + 1][j] = f[i][j] + a[i].second;
        if (j - 1 >= a[i].first) {
          f[i + 1][j] = std::min(f[i + 1][j], f[i][j - 1]);
        }
      }
    }
    printf("%I64d\n", f[n][n]);
  }
  return 0;
}
