#include <bits/stdc++.h>
using namespace std;
using pll = pair<long long, long long>;
long long __gcd(long long a, long long b) {
  return a == 0 ? b : __gcd(b % a, a);
}
long long __lcm(long long a, long long b) { return a / __gcd(a, b) * b; }
int ext_gcd(int, int, int &, int &);
template <typename T, typename T1>
T amax(T &a, T1 b) {
  if (b > a) a = b;
  return a;
}
template <typename T, typename T1>
T amin(T &a, T1 b) {
  if (b < a) a = b;
  return a;
}
const long long INF = 1e9 + 100;
bool good(long long);
const long long N = 1e4 + 1;
long long solve1(long long i, long long t,
                 vector<pair<long long, long long>> &team,
                 vector<vector<long long>> &dp) {
  if (i == 0) {
    if (t >= team[i].first) {
      return 0;
    } else {
      return team[i].second;
    }
  }
  if (dp[i][t] != -1) {
    return dp[i][t];
  }
  long long ans = INF;
  if (t + i >= team[i].first) {
    ans = min(ans, solve1(i - 1, t, team, dp));
  }
  ans = min(ans, team[i].second + solve1(i - 1, t + 1, team, dp));
  return dp[i][t] = ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<pair<long long, long long>> team(n);
    for (long long i = 0; i < n; i++) {
      cin >> team[i].first >> team[i].second;
    }
    sort(team.begin(), team.end());
    vector<vector<long long>> dp(n, vector<long long>(n + 1, -1));
    cout << solve1(n - 1, 0, team, dp) << "\n";
  }
}
