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