#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[200002];
long long dp2[5002][5002];
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(long long));
    dp[0] = 0;
    dp2[n][0] = 0;
    a[n] = {0, 0};
    int llb = 0;
    for (int i = n - 1; i >= 0; --i) {
      int lb = max(a[i].first - i, 0);
      dp[n - i] = a[i].second + dp[n - i - 1];
      for (int j = n - i - 1; j > llb; --j) {
        dp[j] = min(dp[j], dp[j - 1] + a[i].second);
      }
      llb = max(llb, lb);
    }
    for (int i = llb; i <= n; ++i) {
      if (dp[i] == -1) continue;
      ans = min(ans, dp[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;
}
