#include <bits/stdc++.h>
using namespace std;
template <class T>
void printArray(const T* a, int n, ostream& out = cerr);
template <class T>
void printArray(const vector<T>& arr, ostream& out = cerr);
const long long mod = 1e9 + 7;
const double PI = acos(-1);
int n, i, j, k, t;
int s;
pair<int, int> a[200002];
long long dp[5005][5005];
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
      scanf("%d %d", &a[i].first, &a[i].second);
      ans += a[i].second;
    }
    sort(a, a + n);
    memset(dp, -1, (n + 1) * (sizeof dp[0]));
    dp[n][0] = 0;
    for (int i = n - 1; i >= 0; --i) {
      for (int j = max(a[i].first - i - 1, 0); j < n - i; ++j) {
        if (dp[i + 1][j] == -1) continue;
        if (i + j >= a[i].first) {
          if (dp[i][j] == -1) {
            dp[i][j] = dp[i + 1][j];
          } else {
            dp[i][j] = min(dp[i][j], dp[i + 1][j]);
          }
        }
        if (dp[i][j + 1] == -1) {
          dp[i][j + 1] = a[i].second + dp[i + 1][j];
        } else {
          dp[i][j + 1] = min(dp[i][j + 1], a[i].second + dp[i + 1][j]);
        }
      }
    }
    for (int i = 0; i <= n; ++i) {
      if (dp[0][i] == -1) continue;
      ans = min(ans, dp[0][i]);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
template <class T>
void printArray(const T* a, int n, ostream& out) {
  for (int i = 0; i < n; ++i) {
    out << a[i] << " ";
  }
  out << endl;
}
template <class T>
void printArray(const vector<T>& arr, ostream& out) {
  for (const T& x : arr) {
    out << x << " ";
  }
  out << endl;
}
