#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
const int oo = 1e9 + 5;
int du[] = {-1, 0, 0, 1};
int dv[] = {0, -1, 1, 0};
const long long mod = 1e9 + 7;
long long sqr(long long x) { return x * x; }
int n, ntest, tmp, res;
pair<int, long long> a[N + 5];
priority_queue<long long, vector<long long>, greater<long long> > q;
int main() {
  ios_base::sync_with_stdio(false);
  scanf("%d", &ntest);
  for (int j = 1; j <= ntest; j++) {
    tmp = 0;
    res = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d%lld", &a[i].first, &a[i].second);
    for (int i = 1; i <= n; i++) sort(a + 1, a + 1 + n);
    for (int i = n; i >= 1; i--) {
      q.push(a[i].second);
      if (tmp + i - 1 < a[i].first) {
        res += q.top();
        q.pop();
        tmp++;
      }
    }
    while (!q.empty()) q.pop();
    printf("%lld\n", res);
  }
  return 0;
}
