#include <bits/stdc++.h>
using namespace std;
int i, i0, n, m, T, ans;
vector<int> a[200005];
multiset<int> s;
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    s.clear();
    for (i = 1; i <= n; i++) a[i].clear();
    long long ans = 0;
    for (i = 1; i <= n; i++) {
      int m, p;
      scanf("%d %d", &m, &p);
      a[n - m].push_back(p);
      ans += p;
    }
    for (i = n; i; i--) {
      for (int x : a[i]) s.insert(x);
      if (s.size()) {
        ans -= *(--s.end());
        s.erase(--s.end());
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
