#include <bits/stdc++.h>
const int B = 500;
using namespace std;
const long long inf = 1e9 + 12;
long long MOD = 1e9 + 7;
const long long mxn = 5e3 + 5;
const int N = 10000005;
const double pi = 3.14159;
int dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};
long long m, n, k, y, z, q;
pair<int, int> a[mxn];
int dp[mxn][mxn];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> q;
  while (q--) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i].first >> a[i].second;
    }
    sort(a, a + n);
    for (int i = 1; i <= n; i++)
      for (int j = 0; j <= n; j++) dp[i][j] = inf;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= n; j++) {
        if (a[i].first <= j) dp[i + 1][j] = 0;
        dp[i + 1][j] =
            min(dp[i + 1][j], dp[i][min(n, (long long)j + 1)] + a[i].second);
        if (a[i].first <= i + j) dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      }
    }
    cout << dp[n][0] << endl;
  }
}
