#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7, mod2 = 998244353;
unsigned long long base = 131;
const int w = 3e5 + 5;
int n, m, k;
vector<int> s[w];
priority_queue<int, vector<int>, greater<int> > qu;
int main() {
  int i, j, t;
  cin >> t;
  while (t--) {
    while (!qu.empty()) qu.pop();
    long long ans = 0;
    scanf("%d", &n);
    for (i = 1; i <= n; i++) s[i].clear();
    for (i = 1; i <= n; i++) {
      int x, y;
      scanf("%d %d", &x, &y);
      s[x].push_back(y);
    }
    int m = n;
    for (i = n; i >= 1; i--) {
      m -= s[i].size();
      for (j = 0; j < s[i].size(); j++) qu.push(s[i][j]);
      while (m < i) {
        ans += qu.top();
        qu.pop();
        m++;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
