#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    vector<vector<int>> v(n);
    for (int i = 1; i <= n; i++) {
      int m, p;
      scanf("%d %d", &m, &p);
      v[m].emplace_back(p);
    }
    priority_queue<int, vector<int>, greater<int>> qu;
    int has = 0, res = n;
    long long ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (!(int)v[i].size()) continue;
      res -= (int)v[i].size();
      for (auto x : v[i]) qu.push(x);
      while (has + res < i) {
        ans += qu.top();
        qu.pop();
        has++;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
