#include <bits/stdc++.h>
using namespace std;
vector<int> e[200000 + 5];
int main() {
  int T, n;
  int m, p;
  scanf("%d", &T);
  for (int t = 1; t <= T; ++t) {
    scanf("%d", &n);
    for (int i = 0; i <= n; ++i) e[i].clear();
    for (int i = 1; i <= n; ++i) {
      scanf("%d%d", &m, &p);
      e[m].push_back(p);
    }
    priority_queue<int, vector<int>, greater<int> > que;
    long long ans = 0;
    for (int i = n; i >= 0; --i) {
      int len = e[i].size();
      for (int j = 0; j < len; ++j) que.push(e[i][j]);
      while (que.size() > n - i) {
        ans += que.top();
        que.pop();
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
