#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18 + 5;
void solve() {
  int n;
  cin >> n;
  vector<pair<int, int> > vec;
  for (int i = 0; i < n; i++) {
    int m, p;
    cin >> m >> p;
    vec.push_back(make_pair(m, p));
  }
  sort(vec.begin(), vec.end());
  vector<vector<long long> > dp;
  for (int i = 0; i < n + 1; i++) dp.push_back(vector<long long>(n + 1, inf));
  dp[0] = vector<long long>(n + 1, 0ll);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++) {
      if (j <= n - i)
        dp[i][j] = min(dp[i][j], dp[i - 1][j + 1] + vec[i - 1].second);
      if (i + j - 1 >= vec[i - 1].first) dp[i][j] = min(dp[i][j], dp[i - 1][j]);
    }
  }
  cout << dp[n][0] << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  int t;
  cin >> t;
  while (t--) solve();
}
