#include <bits/stdc++.h>
using namespace std;
long long BinPow(long long a, long long n) {
  if (n <= 0) {
    return 1;
  }
  if (n % 2 == 1) {
    return BinPow(a, n - 1) * a;
  }
  long long b = BinPow(a, n / 2);
  return b * b;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<vector<long long>> data(n);
    for (int i = 0; i < n; i++) {
      long long a, b;
      cin >> a >> b;
      data[a].push_back(b);
    }
    multiset<long long> data2;
    long long ans = 0, cnt = 0, pref = n;
    for (int i = n - 1; i >= 0; i--) {
      pref -= data[i].size();
      for (const auto &el : data[i]) {
        data2.insert(el);
      }
      while (i - pref - cnt > 0) {
        ans += *data2.begin();
        data2.erase(data2.begin());
        cnt++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
