#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const double eps = 1e-8;
const int INF32 = 0x3f3f3f3f;
const long long INF64 = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int mod2 = 998244853;
const int N = 2e5 + 5;
const int sz = (1 << 20);
int n;
struct node {
  int m, p;
} peo[N];
bool cmp(node a, node b) { return a.m < b.m; }
vector<int> pp[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    priority_queue<int, deque<int>, greater<int>> Q;
    for (int i = 0; i < n; ++i) {
      cin >> peo[i].m >> peo[i].p;
      pp[peo[i].m].emplace_back(peo[i].p);
    }
    sort(peo, peo + n, cmp);
    long long ans = 0;
    for (int i = n - 1; i >= 0; --i) {
      for (int j : pp[i]) Q.emplace(j);
      while (n - Q.size() < i) {
        ans += Q.top();
        Q.pop();
      }
    }
    cout << ans << '\n';
    for (int i = 0; i < n; ++i) pp[i].clear();
  }
  return 0;
}
