#include <bits/stdc++.h>
using std::greater;
using std::priority_queue;
using std::vector;
int n, x, y, t;
vector<long long> a[200001];
priority_queue<long long, vector<long long>, greater<long long> > q;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) a[i].clear();
    for (int i = 1; i <= n; i++) scanf("%d%d", &x, &y), a[x].push_back(y);
    int m = n, sel = 0, ans = 0;
    for (int i = n; i > 0; i--) {
      m -= a[i].size();
      int x = i - m - sel;
      if (x <= 0) continue;
      for (int j = 0; j < a[i].size(); j++) q.push(a[i][j]);
      while (!q.empty()) {
        if (x)
          ans += q.top(), sel++, x--, q.pop();
        else
          break;
      }
    }
    printf("%d\n", ans);
  }
}
