#include <bits/stdc++.h>
using namespace std;
const int N = 1000010, mod = 1e9 + 7, INF = 0x3f3f3f3f;
const double eps = 1e-6;
int n;
int p[N];
vector<int> v[N];
int main() {
  int _;
  scanf("%d", &_);
  while (_--) {
    scanf("%d", &n);
    for (int i = 0; i <= n; i++) v[i].clear();
    for (int i = 1; i <= n; i++) {
      int x;
      scanf("%d%d", &x, &p[i]);
      v[x].push_back(p[i]);
    }
    int cnt = 0, pre = n;
    long long ans = 0;
    priority_queue<int, vector<int>, greater<int>> q;
    for (int i = n; i >= 1; i--) {
      pre -= v[i].size();
      int need = i - pre;
      for (auto x : v[i]) q.push(x);
      while (cnt < need) {
        ans += q.top();
        q.pop();
        cnt++;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
