#include <bits/stdc++.h>
using namespace std;
int n;
struct node {
  int m, q, id;
  bool operator<(const node& a) const {
    if (q != a.q) return q < a.q;
    return id < a.id;
  }
} p[200010];
bool cmp(node a, node b) {
  if (a.m != b.m) return a.m < b.m;
  return a.q > b.q;
}
multiset<int> s;
vector<int> vec[200010];
int vis[200010];
int main() {
  int T;
  cin >> T;
  while (T--) {
    s.clear();
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
      scanf("%d%d", &p[i].m, &p[i].q);
      vec[i].clear();
    }
    sort(p + 1, p + 1 + n, cmp);
    for (int i = 1; i <= n; ++i) {
      p[i].id = i;
      vec[p[i].m].push_back(p[i].q);
    }
    int pre = n, cnt = 0;
    long long ans = 0;
    for (int i = n; i >= 1; --i) {
      pre -= vec[i].size();
      int need = i - pre;
      for (int j = 0; j < vec[i].size(); ++j) {
        s.insert(vec[i][j]);
      }
      while (cnt < need) {
        ans += *s.begin();
        s.erase(s.begin());
        ++cnt;
      }
    }
    printf("%lld\n", ans);
  }
}
