#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
vector<int> mas[maxn];
multiset<int> se;
signed main() {
  int t, a, b, c, n, m, i, k;
  cin >> t;
  while (t--) {
    cin >> n;
    for (i = 0; i <= n; i++) mas[i].clear();
    for (i = 0; i < n; i++) {
      cin >> a >> b;
      mas[a].push_back(b);
    }
    int b = 0;
    int a = 0;
    long long sum = 0;
    for (i = n; i >= 0; i--) {
      for (auto cur : mas[i]) {
        se.insert(cur);
        b++;
      }
      while (n - b + a < i) {
        sum += *se.begin();
        a++;
        se.erase(se.begin());
      }
    }
    se.clear();
    cout << sum << endl;
  }
}
