#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10;
long long dp[N][N][2];
long long inf = 1e18;
void solve() {
  vector<pair<long long, long long>> v;
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int a, b;
    cin >> a >> b;
    v.push_back({a, b});
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (v[i].first - j < i) {
        dp[i][j][0] = min(dp[i - 1][j][0], dp[i - 1][j][1]);
      } else {
        dp[i][j][0] = inf;
      }
      dp[i][j][1] = min(dp[i - 1][j + 1][0], dp[i - 1][j + 1][1]) + v[i].second;
    }
  }
  cout << min(dp[n][0][0], dp[n][0][1]) << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  ;
  int t;
  cin >> t;
  while (t--) solve();
}
