#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 5;
vector<int> p[M];
priority_queue<int> q;
int t, n, d[M];
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 0; i <= n; i++) p[i].clear();
    while (q.size()) q.pop();
    for (int x, y, i = 1; i <= n; i++) scanf("%d%d", &x, &y), p[x].push_back(y);
    long long res = 0;
    for (int i = n; i; i--) {
      for (auto x : p[i]) q.push(-x);
      while ((int)q.size() > n - i) res -= q.top(), q.pop();
    }
    printf("%I64\n", res);
  }
  return 0;
}
