#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> P) {
  out << '(' << P.first << ',' << P.second << ')';
  return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> V) {
  for (int i = 0; i < ((int)((int)V.size())); i++)
    out << V[i] << ((i != ((int)V.size()) - 1) ? " " : "");
  return out;
}
const long long maxn = 5005;
const long long maxlg = 20;
const long long INF64 = 1e16;
const int INF = 0x3f3f3f3f;
const long long MOD = long long(1e9 + 7);
const long double PI = acos(-1);
const long double eps = 1e-9;
long long mypow(long long a, long long b) {
  long long res = 1LL;
  while (b) {
    if (b & 1) res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
pair<long long, long long> p[maxn];
long long lmt[maxn];
long long dp[maxn][maxn];
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < ((int)n); i++) cin >> p[i].first >> p[i].second;
  sort(p, p + n);
  for (int i = 0; i < ((int)n); i++) {
    if (i == 0 || p[i].first != p[i - 1].first) {
      lmt[i] = max(0LL, p[i].first - i);
    } else
      lmt[i] = 0;
  }
  for (long long i = n - 1; i >= 0; i--) {
    for (long long j = 0; j <= n - i; j++) {
      if (j != 0)
        dp[i][j] = p[i].second + dp[i + 1][j - 1];
      else
        dp[i][j] = p[i].second + dp[i + 1][j];
      if (j != n - i)
        dp[i][j] = min(dp[i][j], dp[i + 1][max(p[i].first - i, j)]);
    }
  }
  cout << dp[0][0] << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) solve();
  return 0;
}
