#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
vector<int> v[maxn];
multiset<int> s;
int main() {
  int t, n, x, y;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    s.clear();
    for (int i = 0; i <= n; i++) v[i].clear();
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &x, &y);
      v[x].push_back(y);
    }
    int sum = n, cnt = 0;
    long long ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      sum -= v[i].size();
      for (auto it : v[i]) s.insert(it);
      int tmp = i - sum;
      while (cnt < tmp) {
        cnt++;
        ans += *s.begin();
        s.erase(s.begin());
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
