#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const long long mod = 1e9 + 7;
int n;
int m[maxn], p[maxn];
vector<int> g[maxn];
int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> n;
    for (int i = 0; i < n; i++) g[i].clear();
    for (int i = 0; i < n; i++) {
      cin >> p[i] >> m[i];
      g[p[i]].push_back(m[i]);
    }
    priority_queue<int, vector<int>, greater<int> > q;
    int pre = n, cnt = 0;
    long long ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      int now = g[i].size();
      pre -= now;
      for (int j : g[i]) q.push(j);
      while (cnt + pre < i) {
        ++cnt;
        ans += q.top();
        q.pop();
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
