#include <bits/stdc++.h>
using namespace std;
long long solve(vector<pair<int, int>>& arr, vector<vector<long long>>& dp,
                int i, int N) {
  int n = arr.size();
  if (i == (int)arr.size()) return 0;
  if (N == n) return 0;
  if (dp[i][N] != -1) return dp[i][N];
  int m = arr[i].first;
  int p = arr[i].second;
  long long res = solve(arr, dp, i + 1, N + 1);
  if (m > N) {
    res += p;
  }
  if ((n - i - 1) + N >= m) {
    res = min(res, solve(arr, dp, i + 1, N));
  }
  return dp[i][N] = res;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t) {
    int n;
    scanf("%d", &n);
    vector<pair<int, int>> arr(n);
    for (int i = 0; i < n; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      arr[i] = {a, b};
    }
    sort(arr.begin(), arr.end(), greater<pair<int, int>>());
    vector<vector<long long>> dp(n + 1, vector<long long>(n + 1, -1));
    long long res = solve(arr, dp, 0, 0);
    cout << res << endl;
    t--;
  }
  return 0;
}
