#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    int f[n + 1];
    deque<pair<int, int> > gg;
    for (int i = 1; i <= n; i++) {
      int a, b;
      cin >> a >> b;
      gg.push_back(make_pair(a, -b));
    }
    sort(gg.begin(), gg.end());
    priority_queue<int, vector<int>, greater<int> > ow;
    for (int i = 0; i < n; i++) {
      while (!gg.empty() and gg.front().first <= i) {
        ow.push(gg.front().second);
        gg.pop_front();
      }
      if (!ow.empty()) {
        f[i] = ow.top();
        ow.pop();
      }
    }
    long long ans = 0;
    while (!ow.empty()) {
      ans += ow.top();
      ow.pop();
    }
    for (auto tmp : gg) {
      ans += tmp.second;
    }
    cout << -ans << endl;
  }
}
