#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int n;
bool cmp(pair<long long, long long> A, pair<long long, long long> B) {
  if (A.first == B.first) return A.second < B.second;
  return A.first > B.first;
}
void solve() {
  scanf("%d", &n);
  vector<pair<long long, long long> > v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i].first >> v[i].second;
  }
  sort(v.begin(), v.end(), cmp);
  long long buy = 0, ans = 0;
  priority_queue<long long> Q;
  for (int i = 0; i < n; i++) {
    Q.push(-v[i].second);
    if (v[i].first - i <= buy)
      continue;
    else {
      buy++;
      ans += -Q.top();
      Q.pop();
    }
  }
  cout << ans << endl;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
  return 0;
}
