#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    for (auto &i : a) cin >> i.first >> i.second;
    sort(a.begin(), a.end());
    vector<vector<long long>> dp(n + 1,
                                 vector<long long>(2 * n + 5, 1LL << 60));
    dp[0][0] = 0;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j <= n; ++j) {
        dp[i + 1][max(j - 1, 0)] =
            min(dp[i + 1][max(j - 1, 0)], dp[i][j] + a[i].second);
        if (i >= a[i].first)
          dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
        else
          dp[i + 1][j + a[i].first - i] =
              min(dp[i + 1][j + a[i].first - i], dp[i][j]);
      }
    cout << dp[n][0] << "\n";
  }
  return 0;
}
