#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 15;
const long long INF = 1e9;
long long n, dp[50][50];
pair<int, long long> a[5001];
long long solve(int i, int j) {
  if (i == n || j >= n) return INF * (j != 0);
  if (dp[i][j] != -1) return dp[i][j];
  int need = max(0, a[i].first - (i + j));
  long long res = min(solve(i + 1, j + need),
                      a[i].second + solve(i + 1, max(0, j + need - 1)));
  return dp[i][j] = res;
}
long long solve() {
  cin >> n;
  for (int i = 0, ggdem = n; i < ggdem; i++) cin >> a[i].first >> a[i].second;
  sort(a, a + n);
  for (int i = 0, ggdem = n; i < ggdem; i++)
    for (int j = 0, ggdem = n; j < ggdem; j++) dp[i][j] = -1;
  return solve(0, 0);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) cout << solve() << "\n";
}
