#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const int N = 2e5 + 7;
const int INF = 1e9 + 7;
int l[N], r[N], ri[N], li[N];
pair<int, int> a[N], b[N];
int t;
bool comp(pair<int, int> a, pair<int, int> b) {
  if (a.first == b.first) return a.second > b.second;
  return a.first < b.first;
}
int main() {
  cin >> t;
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].first, &a[i].second);
    sort(a + 1, a + n + 1);
    priority_queue<int> pq;
    int c = 0;
    long long ans = 0;
    for (int i = n; i >= 1; i--) {
      pq.push(-a[i].second);
      while (!pq.empty() && a[i].first > i - 1 + c) {
        ans += -pq.top();
        pq.pop();
        c++;
      }
    }
    printf("%lld\n", ans);
  }
}
