#include <bits/stdc++.h>
using namespace std;
struct person {
  int m, p;
  person(int _m, int _p) {
    m = _m;
    p = _p;
  }
  bool operator<(person other) const {
    return m < other.m || (m == other.m && p > other.p);
  }
};
int n;
vector<person> v;
void read_input() {
  scanf("%d", &n);
  v.clear();
  int m, p;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &m, &p);
    v.push_back(person(m, p));
  }
  sort(v.begin(), v.end());
}
const long long INF = 1e18;
long long rec(int l, int r, int cnt) {
  if (l == r) {
    return 0;
  }
  long long ans = INF;
  if (r - 1 >= 0 && v[r].m <= (r - l - 1) + cnt) {
    ans = rec(l, r - 1, cnt);
  }
  int new_l = l + 1;
  int new_cnt = cnt + 1;
  while (new_l < r && v[new_l].m <= new_cnt) {
    new_cnt++;
    new_l++;
  }
  if (r - new_l == 0) {
    ans = min(ans, 1LL * v[r].p);
  } else
    ans = min(ans, rec(new_l - 1, r - 1, new_cnt) + v[r].p);
  return ans;
}
void solve() {
  int start_l = 0;
  int cnt = 0;
  while (start_l < n && v[start_l].m <= cnt) {
    cnt++;
    start_l++;
  }
  printf("%lld\n", rec(start_l - 1, n - 1, 0));
}
int main() {
  int z;
  scanf("%d", &z);
  while (z--) {
    read_input();
    solve();
  }
  return 0;
}
