#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(1234);
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int>> mp(n);
    for (auto &e : mp) cin >> e.first >> e.second;
    sort(mp.begin(), mp.end());
    vector<int> left_idx(n);
    for (int i = 0; i < n; ++i) {
      if (i && mp[i - 1].first == mp[i].first) {
        left_idx[i] = left_idx[i - 1];
      } else {
        left_idx[i] = i;
      }
    }
    const ll INF = 1e15 + 10;
    vector<vector<ll>> d(n + 1, vector<ll>(n + 1));
    fill(d[0].begin(), d[0].end(), 0);
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j <= n - i; ++j) {
        auto buy = mp[i - 1].second + d[i - 1][j + 1];
        auto steal = (j >= mp[i - 1].first           ? d[i - 1][j + 1]
                      : j + i - 1 >= mp[i - 1].first ? d[i - 1][j]
                                                     : INF);
        d[i][j] = min(buy, steal);
      }
    }
    cout << d[n][0] << "\n";
  }
}
