#include <bits/stdc++.h>
using namespace std;
int t, n;
struct People {
  int m;
  long long p;
  bool operator>(const People a) const { return p < a.p; }
  bool operator<(const People a) const { return p > a.p; }
} s[200005];
vector<long long> v[200005];
priority_queue<long long, vector<long long>, greater<long long> > q;
bool cmp(People a, People b) {
  if (a.m != b.m) return a.m > b.m;
  return a.p < b.p;
}
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; ++i) v[i].clear();
    for (int i = 1; i <= n; ++i) {
      cin >> s[i].m >> s[i].p;
      v[s[i].m].push_back(s[i].p);
    }
    int cnt = 0, tmp = n;
    long long ans = 0;
    while (q.size()) q.pop();
    for (int i = n - 1; i >= 0; --i) {
      tmp -= v[i].size();
      for (int j = 0; j < (signed)v[i].size(); ++j) {
        q.push(v[i][j]);
      }
      while (cnt + tmp < i) {
        ans += q.top();
        q.pop();
        ++cnt;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
