#include <bits/stdc++.h>
using namespace std;
#pragma GCC diagnostic ignored "-Wmissing-declarations"
inline int safe_mul(const int x, const int y)
    __attribute__((warn_unused_result));
void solve() {
  int n;
  cin >> n;
  vector<pair<int, int>> a(n);
  for (auto& pa : a) {
    cin >> pa.first >> pa.second;
  }
  sort(begin(a), end(a));
  const long long INF = 1000000000000000000LL;
  vector<long long> dp(n, 0);
  for (int step = 0; step < n; ++step) {
    int idx = min(a[step].first - step - 1, n - 2);
    if (idx >= 0) dp[idx] = dp[idx + 1] + a[step].second;
  }
  cout << *max_element(begin(dp), end(dp)) << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) solve();
}
