#include <bits/stdc++.h>
using namespace std;
const int len = 2e5 + 10;
const int mod = 1e9 + 7;
vector<int> ve[len];
priority_queue<int, vector<int>, greater<int> > qu;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) ve[i].clear();
    while (qu.size()) qu.pop();
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
      int m, p;
      scanf("%d%d", &m, &p);
      ve[m].push_back(p);
    }
    for (int i = n - 1; i >= 0; --i) {
      for (int j = 0; j < ve[i].size(); ++j) qu.push(ve[i][j]);
      while (qu.size() > n - i) ans += qu.top(), qu.pop();
    }
    printf("%lld\n", ans);
  }
}
