#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
int t, n;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    vector<pair<int, int>> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i].first >> a[i].second;
    }
    sort(a.begin(), a.begin() + n);
    priority_queue<int, vector<int>, greater<int>> pq;
    int cnt = 0, ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      pq.push(a[i].second);
      if (i + cnt < a[i].first) {
        cnt++;
        ans += pq.top();
        pq.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
