#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
vector<long long> v[maxn];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int test;
  cin >> test;
  int n, m;
  long long p;
  while (test--) {
    cin >> n;
    for (int i = 1; i <= n; i++) v[i].clear();
    for (int i = 1; i <= n; i++) {
      cin >> m >> p;
      v[m].push_back(p);
    }
    long long sol = 0;
    int cant = n;
    multiset<long long> ms;
    int comprados = 0;
    for (int i = n - 1; i >= 1; i--) {
      cant -= v[i].size();
      int need = i - cant - comprados;
      for (auto j : v[i]) ms.insert(j);
      int cont = 0;
      while (cont < need) {
        cont++;
        comprados++;
        sol += *ms.begin();
        ms.erase(ms.begin());
      }
    }
    cout << sol << "\n";
  }
  return 0;
}
