#include <bits/stdc++.h>
using namespace std;
ifstream in("inp.txt");
ofstream out("out.txt");
const long long INF = 4e18 + 7;
void solve() {
  int n;
  cin >> n;
  vector<vector<int>> pw(n);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    pw[x].push_back(y);
  }
  int tsum = n;
  int tkol = 0;
  multiset<int> pr;
  long long ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (pw.empty()) continue;
    tsum -= ((int)pw[i].size());
    int tmp = i - tsum;
    for (auto el : pw[i]) pr.insert(el);
    while (tkol < tmp && !pr.empty()) {
      tkol++;
      ans += *(pr.begin());
      pr.erase(pr.begin());
    }
  }
  cout << ans << '\n';
}
signed main() {
  ios::sync_with_stdio(0);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
