#include <bits/stdc++.h>
using std::vector;
const long long N = 3e5;
namespace Heap {
bool cmp(long long a, long long b) { return a < b; }
struct heap {
  long long h[N + 10], sz;
  heap() { sz = 0; }
  void push(long long x) {
    h[++sz] = x, std::push_heap(h + 1, h + 1 + sz, cmp);
  }
  void pop() { std::pop_heap(h + 1, h + 1 + sz, cmp), sz--; }
  long long top() { return h[1]; }
  long long size() { return sz; }
} q;
}  // namespace Heap
using namespace Heap;
long long n, pre, cnt, ans;
vector<long long> a[N + 10];
void clr() {
  for (long long i = 0; i <= n; i++) a[i].clear();
  while (q.size()) q.pop();
}
signed main() {
  long long T;
  scanf("%lld", &T);
  while (T--) {
    scanf("%lld", &n), clr(), pre = n, cnt = ans = 0;
    for (long long i = 1, c, t; i <= n; i++)
      scanf("%lld%lld", &c, &t), a[c].push_back(t);
    for (long long i = n; ~i; i--) {
      pre -= a[i].size();
      for (long long j = 0; j < a[i].size(); j++) q.push(a[i][j]);
      while (pre + cnt < i) ans += q.top(), q.pop(), cnt++;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
