#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int N = 2e5 + 5;
vector<int> v[N];
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) v[i].clear();
  for (int i = 0; i < n; i++) {
    int x, s;
    cin >> x >> s;
    v[x].emplace_back(s);
  }
  LL result = 0;
  multiset<int> q;
  int cnt = 0;
  int pref = n;
  for (int i = n - 1; i >= 0; i--) {
    pref -= v[i].size();
    int need = i - pref;
    for (auto x : v[i]) q.insert(x);
    while (cnt < need) {
      result += *q.begin();
      q.erase(q.begin());
      cnt++;
    }
  }
  cout << result << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) solve();
}
