#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
template <class T>
inline void chmax(T &a, const T &b) {
  if ((a) < (b)) (a) = (b);
}
template <class T>
inline void chmin(T &a, const T &b) {
  if ((a) > (b)) (a) = (b);
}
template <typename T>
using PQ = priority_queue<T, vector<T>, greater<T>>;
struct Fast {
  Fast() {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
  }
} fast;
long long query() {
  long long n;
  cin >> n;
  vector<long long> p(n), m(n);
  vector<pair<long long, long long>> pm;
  for (long long i = (long long)(0); i < (long long)(n); ++i) {
    cin >> m[i] >> p[i];
    pm.push_back(make_pair(m[i], p[i]));
  }
  sort((pm).begin(), (pm).end());
  for (long long i = (long long)(0); i < (long long)(n); ++i) {
    m[i] = pm[i].first;
    p[i] = pm[i].second;
  }
  42;
  42;
  vector<vector<long long>> dp(n + 1, vector<long long>(n + 3, INF));
  dp[0][0] = 0;
  for (long long i = (long long)(0); i < (long long)(n); ++i) {
    for (long long c = (long long)(0); c < (long long)(n); ++c) {
      if (dp[i][c] == INF) continue;
      chmin(dp[i + 1][max(c - 1, 0LL)], dp[i][c] + p[i]);
      long long add = m[i] - (c + i);
      if (add <= 0) {
        chmin(dp[i + 1][c], dp[i][c]);
      } else {
        42;
        assert(c + add <= n);
        chmin(dp[i + 1][c + add], dp[i][c]);
      }
    }
  }
  42;
  cout << dp[n][0] << endl;
  cerr << endl;
  return 0;
}
long long solve() {
  long long Q;
  cin >> Q;
  for (long long _ = (long long)(0); _ < (long long)(Q); ++_) {
    query();
  }
  return 0;
}
signed main() {
  solve();
  return 0;
}
