#include <bits/stdc++.h>
using namespace std;
pair<int, int> v[5005];
long long dp[5001][5001];
long long DP(int i, int taken) {
  if (i == 1) {
    if (v[i].first <= taken) return 0;
    return v[i].second;
  }
  if (dp[i][taken] != -1) return dp[i][taken];
  long long ret = INT_MAX;
  if (taken + i > v[i].first) ret = DP(i - 1, taken);
  ret = min(ret, DP(i - 1, taken + 1) + v[i].second);
  return dp[i][taken] = ret;
}
int main() {
  int t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> v[i].first >> v[i].second;
    sort(v + 1, v + 1 + n);
    for (int i = 1; i <= n; i++) memset(dp[i], -1, sizeof dp[i]);
    cout << DP(n, 0) << endl;
  }
}
