#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
const int maxt = maxn << 1;
const long long INF = 1LL << 60;
int n, Mx;
struct anode {
  int x, y;
  anode() {}
  anode(int _x, int _y) : x(_x), y(_y) {}
} a[maxn];
vector<int> g[maxn];
int arr[maxn], suc[maxn];
long long sum[maxn];
priority_queue<int> q;
inline void solve() {
  scanf("%d", &n);
  Mx = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].x, &a[i].y);
    g[a[i].x].push_back(a[i].y);
    Mx = max(Mx, a[i].x);
  }
  for (int i = 0; i <= Mx; i++) sort(g[i].begin(), g[i].end());
  arr[0] = g[0].size();
  suc[Mx + 1] = 0;
  for (int i = 1; i <= Mx; i++) arr[i] = arr[i - 1] + g[i].size();
  for (int i = Mx; i >= 0; i--) suc[i] = suc[i + 1] + g[i].size();
  int cnt = 0;
  long long Ans = 0;
  while (!q.empty()) q.pop();
  for (int i = Mx; i >= 1; i--) {
    int Sz = g[i].size();
    if (!Sz) continue;
    for (int j = 0; j < Sz; j++) q.push(-g[i][j]);
    int num = i - arr[i - 1];
    while (cnt < num) {
      Ans += (long long)-q.top();
      q.pop();
      cnt++;
    }
  }
  printf("%lld\n", Ans);
  for (int i = 1; i <= n; i++) {
    vector<int>().swap(g[a[i].x]);
  }
}
int main() {
  int cas;
  scanf("%d", &cas);
  while (cas--) {
    solve();
  }
  return 0;
}
