#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int tt, qq = 0;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<vector<int>> A(n);
    int x, y;
    for (int i = 0; i < n; i++) {
      cin >> y >> x;
      A[y].emplace_back(x);
    }
    priority_queue<int, vector<int>, greater<int>> Q;
    int rest = n;
    long long ans = 0;
    int cnt = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (A.size() == 0) {
        continue;
      }
      for (auto c : A[i]) {
        Q.push(c);
      }
      rest -= A[i].size();
      if (cnt + rest < i) {
        for (int j = cnt + rest; j < i; j++) {
          ans += Q.top();
          Q.pop();
          cnt++;
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
