#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int N = 2e5 + 2;
int n, s[N], cnt;
long long res;
vector<int> a[N];
priority_queue<int> q;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    while (q.size()) q.pop();
    cin >> n;
    res = cnt = 0;
    for (int _b = n, i = 0; i <= _b; ++i) s[i] = 0, a[i].clear();
    for (int _b = n, i = 1; i <= _b; ++i) {
      int m, p;
      cin >> m >> p;
      a[m].push_back(p);
    }
    s[0] = a[0].size();
    for (int _b = n - 1, i = 1; i <= _b; ++i) s[i] = s[i - 1] + a[i].size();
    for (int _b = 1, i = n; i >= _b; --i) {
      for (auto v : a[i]) q.push(-v);
      int delta = (i - s[i - 1] - cnt);
      while (delta > 0) {
        --delta;
        ++cnt;
        res += -q.top();
        q.pop();
      }
    }
    cout << res << '\n';
  }
  return 0;
}
