#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int T, n;
vector<int> g[N];
long long ans;
inline void fr(int &num) {
  num = 0;
  char c = getchar();
  int p = 1;
  while (c < '0' || c > '9') c == '-' ? p = -1, c = getchar() : c = getchar();
  while (c >= '0' && c <= '9') num = num * 10 + c - '0', c = getchar();
  num *= p;
}
priority_queue<int, vector<int>, greater<int> > q;
int main() {
  fr(T);
  while (T--) {
    fr(n), ans = 0;
    for (register int i = 1, x, y; i <= n; ++i) fr(x), fr(y), g[x].push_back(y);
    for (register int i = n - 1; i >= 0; --i) {
      for (register int j = 0, sz = g[i].size(); j < sz; ++j) q.push(g[i][j]);
      while (q.size() > n - i) ans += q.top(), q.pop();
    }
    printf("%I64d\n", ans);
    for (register int i = 0; i < n; ++i) g[i].clear();
    while (!q.empty()) q.pop();
  }
  return 0;
}
