#include <bits/stdc++.h>
#pragma G++ optimize "O2"
#pragma GCC optimize "O2"
#pragma G++ optimize "O3"
#pragma GCC optimize "O3"
#pragma G++ optimize "Ofast"
#pragma GCC optimize "Ofast"
using namespace std;
int t;
vector<int> vec[200010];
int n;
long long ans = 0;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    ans = 0;
    for (register int i = 0; i <= n; i++) vec[i].clear();
    for (register int i = 1; i <= n; i++) {
      int m, p;
      scanf("%d%d", &m, &p);
      vec[m].push_back(p);
    }
    priority_queue<int, vector<int>, greater<int> > q;
    for (register int i = n - 1; i >= 0; i--) {
      for (register int j = 0; j < vec[i].size(); j++) {
        q.push(vec[i][j]);
      }
      while (q.size() > n - i) {
        ans += q.top();
        q.pop();
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
