#include <bits/stdc++.h>
using namespace std;
long long call(int i, int m, vector<pair<int, int> >& voters,
               vector<vector<long long> >& dp) {
  int n = voters.size();
  if (i == 0) {
    if (voters[i].first <= m) {
      return 0;
    }
    return voters[i].second;
  }
  if (dp[i][m] != -1) {
    return dp[i][m];
  }
  long long ans = LLONG_MAX;
  if (voters[i].first <= m) {
    ans = min(ans, call(i - 1, m + 1, voters, dp));
  } else if (voters[i].first <= m + i) {
    ans = min(ans, call(i - 1, m, voters, dp));
  }
  ans = min(ans, voters[i].second + call(i - 1, m + 1, voters, dp));
  return dp[i][m] = ans;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int> > voters;
    vector<vector<long long> > dp(n, vector<long long>(n, -1));
    int m, p;
    for (int i = 0; i < n; i++) {
      cin >> m >> p;
      voters.push_back(make_pair(m, p));
    }
    sort(voters.begin(), voters.end());
    cout << call(n - 1, 0, voters, dp) << "\n";
  }
}
