#include <bits/stdc++.h>
using namespace std;
vector<long long> V[200000 + 100];
int main() {
  int T;
  cin >> T;
  int n, m;
  long long p;
  while (T--) {
    scanf("%d", &n);
    for (int i = 0; i <= n; i++) V[i].clear();
    for (int i = 0; i < n; i++) {
      scanf("%d%lld", &m, &p);
      V[m].push_back(p);
    }
    multiset<long long> ms;
    ms.clear();
    int cnt = 0;
    int ff = n;
    long long ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      ff -= V[i].size();
      for (int j = 0; j < V[i].size(); j++) {
        ms.insert(V[i][j]);
      }
      while (cnt < i - ff) {
        cnt++;
        ans += *ms.begin();
        ms.erase(ms.begin());
      }
    }
    printf("%lld\n", ans);
  }
}
