#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    int n;
    cin >> n;
    pair<long long, long long> a[n];
    for (int j = 0; j < n; ++j) {
      cin >> a[j].first >> a[j].second;
    }
    sort(a, a + n);
    long long dp[n + 1][n + 1];
    for (int k = 0; k < n + 1; ++k) {
      for (int j = 0; j < n + 1; ++j) {
        dp[k][j] = (long long)1e18;
      }
    }
    for (int i1 = 0; i1 < n + 1; ++i1) {
      dp[0][i1] = 0;
    }
    for (int l = 1; l < n + 1; ++l) {
      for (int j = n - 1; j >= 0; --j) {
        dp[l][j] = min(dp[l - 1][j + 1] + a[l - 1].second, dp[l][j]);
        long long need = max(0ll, a[l - 1].first - j);
        if (need >= l) continue;
        dp[l][j] = min(dp[l][j], dp[need][j]);
      }
    }
    cout << dp[n][0] << "\n";
  }
}
