#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5678;
const long long INF = (long long)1e13;
long long dp[MAXN];
bool OK[MAXN];
int main() {
  int T;
  scanf("%d", &T);
  for (int t = 1; t <= T; t++) {
    int N;
    scanf("%d", &N);
    vector<pair<int, int> > V;
    for (int i = 0; i < N; i++) {
      int m, p;
      scanf("%d %d", &m, &p);
      V.push_back(make_pair(m, p));
    }
    sort(V.begin(), V.end());
    memset(dp, 0, sizeof(dp));
    memset(OK, 0, sizeof(OK));
    OK[0] = 1;
    for (int i = N - 1; i >= 0; i--) {
      int need = V[i].first - i;
      long long w = V[i].second;
      if (need < 0) need = 0;
      for (int j = N; j >= max(1, need); j--) {
        if (OK[j - 1]) {
          if (!OK[j])
            dp[j] = dp[j - 1] + w;
          else
            dp[j] = min(dp[j], dp[j - 1] + w);
          OK[j] = 1;
        }
      }
      for (int j = need - 1; j >= 1; j--) {
        if (OK[j - 1]) {
          OK[j] = 1;
          dp[j] = dp[j - 1] + w;
        } else
          OK[j] = false;
      }
      if (need) OK[0] = false;
      for (int j = 0; j <= N; j++) {
        if (!OK[j]) continue;
      }
    }
    long long ans = INF;
    for (int i = 0; i <= N; i++) {
      if (OK[i]) {
        ans = min(ans, dp[i]);
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
