#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using ld = long double;
const ll mod = 1000000007;
const ll INF = 1000000099;
const ll LINF = (ll)(1e18 + 99);
vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
template <typename T, typename U>
void chmin(T& t, const U& u) {
  if (t > u) t = u;
}
template <typename T, typename U>
void chmax(T& t, const U& u) {
  if (t < u) t = u;
}
template <typename T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <typename T>
T mpow(T a, T n) {
  T res = 1;
  for (; n; n >>= 1) {
    if (n & 1) res = res * a;
    a = a * a;
  }
  return res;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    ll ans = 0;
    vector<pair<ll, ll> > v(n);
    for (ll i = 0; i < (ll)(n); i++) {
      cin >> v[i].first >> v[i].second;
      ans += v[i].second;
    }
    sort((v).begin(), (v).end());
    int ind = 0;
    priority_queue<ll> q;
    for (ll i = 0; i < (ll)(n); i++) {
      while (ind < n && v[ind].first <= i) {
        q.push(v[ind].second);
        ind++;
      }
      if (!q.empty()) {
        ans -= q.top();
        q.pop();
      }
    }
    cout << ans << endl;
  }
}
