#include <bits/stdc++.h>
using namespace std;
using lint = long long;
void solve() {
  int n;
  scanf("%d", &n);
  vector<vector<int>> Ev(n);
  for (int i = 0; i < (n); i++) {
    int m, p;
    scanf("%d%d", &m, &p);
    Ev[m].emplace_back(p);
  }
  vector<int> sum(n + 1);
  for (int x = 0; x < (n); x++) sum[x + 1] += sum[x] + Ev[x].size();
  lint ans = 0;
  int bought = 0;
  multiset<int> Pending;
  for (int x = n - 1; x >= 0; x--) {
    Pending.insert(Ev[x].begin(), Ev[x].end());
    while (sum[x] + bought < x) {
      bought++;
      ans += *Pending.begin();
      Pending.erase(Pending.begin());
    }
  }
  printf("%lld\n", ans);
}
int main() {
  int q;
  scanf("%d", &q);
  for (int _ = 0; _ < (q); _++) solve();
  return 0;
}
