#include <bits/stdc++.h>
using namespace std;
const int maxn = 300050;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
priority_queue<int, vector<int>, greater<int>> q, p;
vector<int> G[maxn];
int main() {
  int n, i, j, t, m, l, r, x, y, z, k;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    q = p;
    for (i = 0; i < n; i++) G[i].clear();
    for (i = 0; i < n; i++) {
      scanf("%d %d", &x, &y);
      G[x].push_back(y);
    }
    x = n;
    y = 0;
    long long ans = 0;
    for (i = n - 1; i >= 0; i--) {
      x -= G[i].size();
      k = i - x;
      for (auto v : G[i]) q.push(v);
      while (y < k) {
        ans += q.top();
        q.pop();
        y++;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
