#include <bits/stdc++.h>
using namespace std;
int dp[5005][5005];
pair<int, int> a[5005];
int get(int ind, int cnt) {
  if (dp[ind][cnt] != -1) return dp[ind][cnt];
  if (ind == 1) {
    if (cnt >= a[ind].first)
      return dp[ind][cnt] = 0;
    else
      return dp[ind][cnt] = a[ind].second;
  }
  int ans = 1e9;
  if (cnt >= a[ind].first)
    ans = min(ans, get(ind - 1, cnt + 1));
  else if (cnt + ind - 1 >= a[ind].first)
    ans = min(ans, get(ind - 1, cnt));
  ans = min(ans, a[ind].second + get(ind - 1, cnt + 1));
  return dp[ind][cnt] = ans;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
      for (int j = 0; j <= n; j++) dp[i][j] = -1;
    for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
    sort(a + 1, a + n + 1);
    cout << get(n, 0) << "\n";
  }
}
