#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
int n;
struct T {
  int m, p;
};
T a[N];
bool lf(T a, T b) {
  if (a.m != b.m) return a.m < b.m;
  return a.p < b.p;
}
void Sol() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i].m >> a[i].p;
  sort(a + 1, a + n + 1, lf);
  priority_queue<int, vector<int>, greater<int>> pq;
  int now = 0, res = 0;
  for (int i = n; i >= 1; i--) {
    pq.push(a[i].p);
    if (a[i].m - i + 1 <= now)
      continue;
    else {
      now++;
      res += pq.top();
      pq.pop();
    }
  }
  cout << res << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) Sol();
}
