#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int to, nxt;
} e[300010];
int T, fir[300010], cnt, n;
struct heap {
  int Q[300010], sz;
  void push(int x) {
    Q[++sz] = x;
    push_heap(Q + 1, Q + sz + 1);
  }
  void pop() {
    pop_heap(Q + 1, Q + sz + 1);
    sz--;
  }
  int top() { return Q[1]; }
  bool empty() { return sz == 0; }
} q;
long long ans;
inline void add(int x, int y) {
  e[++cnt].to = y;
  e[cnt].nxt = fir[x];
  fir[x] = cnt;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 0; i <= n + 1; i++) fir[i] = 0;
    ans = cnt = 0;
    for (int i = 1, c, t; i <= n; i++)
      scanf("%d%d", &c, &t), add(c + 1, t), ans += t;
    q.sz = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = fir[i]; j; j = e[j].nxt) q.push(e[j].to);
      if (!q.empty()) ans -= q.top(), q.pop();
    }
    printf("%lld\n", ans);
  }
  return 0;
}
