#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
vector<int> f[maxn];
priority_queue<int, vector<int>, greater<int> > q;
int main() {
  int t, n, x, y;
  long long ans;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) f[i].clear();
    for (int i = 0; i < n; ++i) {
      scanf("%d%d", &x, &y);
      f[x].push_back(y);
    }
    while (!q.empty()) q.pop();
    x = n;
    ans = y = 0;
    for (int i = n - 1; i >= 0; i--) {
      x -= f[i].size();
      for (auto e : f[i]) q.push(e);
      while (x + y < i) {
        ans += q.top();
        q.pop();
        y++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
