#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long inf_ll = 1e18;
const int maxn = 200005;
pair<long long, long long> a[maxn];
priority_queue<long long> pq;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long out = 0;
    for (int i = 0; i < n; i++) {
      auto& [m, p] = a[i];
      cin >> m >> p;
      out += p;
    }
    sort(a, a + n);
    int x = 0;
    for (int i = 0; i < n; i++) {
      while (x < n && a[x].first <= i) pq.push(a[x++].second);
      if (!pq.empty()) {
        out -= pq.top();
        pq.pop();
      }
    }
    cout << out << "\n";
    while (!pq.empty()) pq.pop();
  }
}
