#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int vs[N + 2];
pair<int, int> pp[N + 2];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) vs[i] = 0;
    for (int i = 1; i <= n; i++) cin >> pp[i].first >> pp[i].second;
    sort(pp + 1, pp + n + 1);
    priority_queue<int> tk;
    for (int i = 1; i <= n; i++) tk.push(0);
    set<pair<int, int> > rm;
    for (int i = 1; i <= n; i++) rm.insert({pp[i].second, i});
    int sz = 0;
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      if (sz >= pp[i].first) {
        if (vs[i]) {
          tk.push(pp[i].second);
          continue;
        }
        rm.erase({pp[i].second, i});
        sz++;
        continue;
      }
      pair<int, int> p = *rm.begin();
      rm.erase(p);
      i--;
      int an = tk.top();
      ans += p.first - an;
      sz++;
      vs[p.second] = 1;
    }
    cout << ans << "\n";
  }
  return 0;
}
