#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int n;
vector<int> v[1 + N];
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) v[i].clear();
  for (int i = 1; i <= n; i++) {
    int m, p;
    cin >> m >> p;
    v[m].push_back(p);
  }
  long long ans = 0;
  int cur = 0;
  priority_queue<int> pq;
  for (int i = n; i >= 0; i--) {
    for (auto x : v[i]) pq.push(-x);
    cur += v[i].size();
    while (i + cur > n) {
      ans += -pq.top();
      pq.pop();
      cur--;
    }
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
