#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
vector<int> v[maxn];
int main() {
  int t;
  cin >> t;
  int n;
  int m, p;
  while (t--) {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      v[i].clear();
    }
    int m, p;
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &m, &p);
      v[m].push_back(p);
    }
    long long sum = 0;
    int have = 0, res = n;
    priority_queue<int, vector<int>, greater<int>> q;
    for (int i = n - 1; i >= 0; i--) {
      res -= v[i].size();
      for (auto p : v[i]) q.push(p);
      while (have + res < i) {
        sum += q.top();
        q.pop();
        have++;
      }
    }
    cout << sum << endl;
  }
}
