#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 7, P = 1e9 + 7;
long long read() {
  long long s = 0, w = 1;
  char c;
  while (c = getchar(), c > '9' || c < '0')
    if (c == '-') w = -1;
  while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
  return s * w;
}
void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
}
void wsp(long long x) {
  write(x);
  putchar(' ');
}
void wln(long long x) {
  write(x);
  putchar('\n');
}
struct node {
  long long x, y;
  bool operator<(node a) const { return y > a.y; }
} p[N];
long long T, n, ans, cnt;
long long s[N];
priority_queue<long long, vector<long long>, greater<long long> > q;
vector<long long> V[N];
void solve() {
  for (long long i = 0; i <= n; i++) V[i].clear(), s[i] = 0;
  while (!q.empty()) q.pop();
  n = read();
  ans = 0;
  cnt = 0;
  for (long long i = 1, x, y; i <= n; i++) {
    x = read();
    y = read();
    V[x].push_back(y);
  }
  for (long long i = 1; i <= n; i++) s[i] = s[i - 1] + V[i - 1].size();
  for (long long i = n; i; i--) {
    for (long long j = 0; j < V[i].size(); j++) q.push(V[i][j]);
    if (V[i].size() && cnt + s[i] < i) {
      while (cnt + s[i] < i) {
        ans += q.top();
        q.pop();
        cnt++;
      }
    }
  }
  printf("%lld\n", ans);
}
signed main() {
  T = read();
  while (T--) solve();
}
