#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int, int> > voters;
pair<long long, int> dp[5005][5005];
int visit[5005];
bool comp(const pair<int, int>& a, const pair<int, int>& b) {
  return a.first < b.first;
}
bool comp1(const pair<int, int>& a, const pair<int, int>& b) {
  if (a.first == b.first) {
    return a.second < b.second;
  }
  return a.first < b.first;
}
pair<long long, int> call(int m, int i) {
  if (m == n) {
    return make_pair(0, 1);
  }
  if (i == n) {
    return make_pair(0, m == n ? 1 : 0);
  }
  if (dp[m][i].first != -1) {
    return dp[m][i];
  }
  long long ans = 1000000000000000007ll;
  pair<long long, int> ans1, ans2;
  int index;
  index =
      upper_bound(voters.begin(), voters.end(), make_pair(m + 1, -1), comp) -
      voters.begin();
  if (index <= i) {
    index = upper_bound(voters.begin(), voters.end(), make_pair(index + 1, -1),
                        comp) -
            voters.begin();
  }
  if (index == 0) index = 1;
  ans1 = call(index, i + 1);
  ans2 = call(m, i + 1);
  if (ans1.second) {
    if (voters[i].first <= m) {
      ans = min(ans, ans1.first);
    } else {
      ans = min(ans, voters[i].second + ans1.first);
    }
  }
  if (ans2.second) {
    ans = min(ans, ans2.first);
  }
  return dp[m][i] = ans == 1000000000000000007ll ? make_pair(ans, 0)
                                                 : make_pair(ans, 1);
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    voters.clear();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) dp[i][j] = make_pair(-1, 0);
    }
    fill(visit, visit + n, 0);
    int m, p;
    for (int i = 0; i < n; i++) {
      cin >> m >> p;
      voters.push_back(make_pair(m, p));
    }
    sort(voters.begin(), voters.end(), comp1);
    long long ans = call(0, 0).first;
    cout << ans << "\n";
  }
}
