#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-') ch = getchar();
  if (ch == '-') f = -1, ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return x * f;
}
void write(long long x) {
  if (x == 0) {
    putchar('0'), putchar('\n');
    return;
  }
  int f = 0;
  char ch[20];
  if (x < 0) putchar('-'), x = -x;
  while (x) ch[++f] = x % 10 + '0', x /= 10;
  while (f) putchar(ch[f--]);
  putchar('\n');
  return;
}
priority_queue<long long> q;
pair<int, long long> a[200007];
int n, t, num;
long long ans;
int main() {
  t = read();
  while (t--) {
    n = read(), num = ans = 0;
    for (register int i = (1); i <= (n); ++i)
      a[i].first = read(), a[i].second = read();
    sort(a + 1, a + n + 1);
    for (register int i = (n); i >= (1); --i) {
      q.push(-a[i].second);
      while (i - 1 + num < a[i].first) ans -= q.top(), q.pop(), num++;
    }
    write(ans);
    while (!q.empty()) q.pop();
  }
  return 0;
}
