#include <bits/stdc++.h>
using namespace std;
class Task {
 public:
  void minify(long long& x, long long y) {
    if (x == -1 || x > y) {
      x = y;
    }
  }
  void solve() {
    int n;
    cin >> n;
    vector<array<int, 2>> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i][0] >> a[i][1];
    }
    sort(a.begin(), a.end());
    vector<vector<long long>> dp(n + 1, vector<long long>(n + 1, -1));
    dp[n][0] = 0;
    for (int i = n - 1; i >= 0; i--)
      for (int j = 0; j <= n - i; j++) {
        if (dp[i + 1][max(0, j - 1)] != -1) {
          dp[i][j] = dp[i + 1][max(0, j - 1)] + a[i][1];
        }
        if (j + i >= a[i][0] && dp[i + 1][j] != -1) {
          minify(dp[i][j], dp[i + 1][j]);
        }
        if (j + i < a[i][0]) {
          minify(dp[i][j], dp[i + 1][j + a[i][0] - i - j]);
        }
      }
    cout << dp[0][0] << '\n';
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  cin >> T;
  for (int t = 1; t <= T; t++) {
    (new Task())->solve();
  }
  return 0;
}
