#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
int T, n;
struct gg {
  int m, p;
} g[200015];
bool cmp1(const gg &g1, const gg &g2) { return g1.m < g2.m; }
struct cmp2 {
  bool operator()(const gg &g1, const gg &g2) { return g1.p < g2.p; }
};
priority_queue<gg, vector<gg>, cmp2> que;
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
      scanf("%d%d", &g[i].m, &g[i].p);
    }
    sort(g + 1, g + n + 1, cmp1);
    for (int i = 1, j = 1; i <= n; ++i) {
      while (j <= n && g[j].m < i) {
        que.push(g[j++]);
      }
      if (que.size()) que.pop();
    }
    long long ans = 0;
    while (que.size()) {
      ans += que.top().p;
      que.pop();
    }
    printf("%lld\n", ans);
  }
  return 0;
}
