#include <bits/stdc++.h>
using namespace std;
vector<int> g[200005];
priority_queue<int, vector<int>, greater<int> > qu;
int main() {
  int T, n, x, y;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
      scanf("%d%d", &x, &y);
      g[x].push_back(y);
    }
    long long ans = 0;
    for (int i = n - 1; i >= 0; --i) {
      for (int j = 0, sz = g[i].size(); j < sz; ++j) qu.push(g[i][j]);
      while (qu.size() > n - i) ans += qu.top(), qu.pop();
    }
    printf("%lld\n", ans);
    for (int i = 0; i < n; ++i) g[i].clear();
    while (!qu.empty()) qu.pop();
  }
  return 0;
}
