#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5000;
int n, testNumber;
pair<int, int> voter[MAXN];
long long int dp[MAXN + 1][MAXN + 1];
void prep();
void read();
void init();
void setBase();
void debug();
void print();
int main() {
  prep();
  cin >> testNumber;
  while (testNumber--) {
    read();
    init();
    print();
  }
  return 0;
}
void prep() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void read() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> voter[i].first >> voter[i].second;
}
void init() {
  sort(voter, voter + n);
  setBase();
  for (int m = n - 1; m >= 0; m--) {
    for (int k = 0; k <= n; k++) {
      dp[m][k] = (long long int)1e18;
      if (voter[m].first <= m)
        dp[m][k] = min(dp[m][k], dp[m + 1][k]);
      else {
        int debt = voter[m].first - m;
        if (k + debt <= n) dp[m][k] = min(dp[m][k], dp[m + 1][k + debt]);
      }
      if (k > 0) dp[m][k] = min(dp[m][k], dp[m + 1][k - 1] + voter[m].second);
    }
  }
}
void setBase() {
  dp[n][0] = 0;
  for (int k = 1; k <= n; k++) dp[n][k] = (long long int)1e18;
}
void debug() {
  for (int i = 0; i < n; i++)
    cerr << "Voter[" << i + 1 << "] = (" << voter[i].first << " , "
         << voter[i].second << ")" << endl;
  cerr << "***************************************************" << endl;
  for (int m = n; m >= 0; m--) {
    for (int k = 0; k <= n; k++)
      cerr << "DP[" << m + 1 << "][" << k << "] = " << dp[m][k] << endl;
    cerr << "***************************************************" << endl;
  }
}
void print() {
  long long int ans = (long long int)1e18;
  for (int k = 0; k <= n; k++) ans = min(dp[0][k], ans);
  cout << ans << endl;
}
