#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, T;
vector<int> buf[(200010)];
priority_queue<int, vector<int>, greater<int> > q;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch == '-' ? f = -1, ch = getchar() : ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return x * f;
}
signed main() {
  T = read();
  while (T--) {
    n = read();
    int ans = 0, pre = n, cnt = 0, x;
    for (int i = 0; i < n; ++i) buf[i].clear();
    for (int i = 1; i <= n; ++i) x = read(), buf[x].push_back(read());
    while (!q.empty()) q.pop();
    for (int i = n - 1; i >= 0; --i) {
      pre -= buf[i].size();
      for (int j = 0; j < (int)buf[i].size(); ++j) q.push(buf[i][j]);
      while (cnt < i - pre) ans += q.top(), ++cnt, q.pop();
    }
    printf("%d\n", ans);
  }
  return 0;
}
