#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int> > mp(n);
    int maxm = 0;
    for (int i = 0; i < n; i++) {
      cin >> mp[i].first >> mp[i].second;
      if (mp[i].first > maxm) maxm = mp[i].first;
    }
    sort(mp.begin(), mp.end());
    vector<vector<long long> > dp;
    dp.resize(maxm + 1, vector<long long>(n, 0));
    for (int i = 0; i <= maxm; i++) {
      if (i < mp[0].first) dp[i][0] = mp[0].second;
    }
    for (int i = maxm - 1; i >= 0; i--) {
      for (int j = 1; j < n; j++) {
        dp[i][j] = dp[i + 1][j - 1] + mp[j].second;
        if (mp[j].first - i <= j) dp[i][j] = min(dp[i][j], dp[i][j - 1]);
      }
    }
    cout << dp[0][n - 1] << endl;
    cout.flush();
  }
}
