#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long n, m, k, l, r, i, j, dp[5001][5001];
string s;
string v1, v0;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<pair<long long, long long> > v;
    v.push_back(make_pair(0, 0));
    for (int i = 0; i < n; i++) {
      long long x, y;
      cin >> x >> y;
      v.push_back(make_pair(x, y));
    }
    sort(v.begin(), v.end());
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++) dp[i][j] = 1e18;
    dp[0][0] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= n; j++)
        if (dp[i][j] != 1e18) {
          dp[i + 1][j + max(0ll, v[i + 1].first - i)] =
              min(dp[i + 1][j + max(0ll, v[i + 1].first - i)], dp[i][j]);
          dp[i + 1][j] = min(dp[i][j] + v[i + 1].second, dp[i + 1][j]);
          if (j)
            dp[i + 1][j - 1] =
                min(dp[i + 1][j - 1], dp[i][j] + v[i + 1].second);
        }
    long long ans = dp[n][0];
    cout << ans << "\n";
  }
  return 0;
}
