#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> ver[N];
int main() {
  ios ::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) ver[i].clear();
    for (int i = 0; i < n; i++) {
      int c, x;
      cin >> c >> x;
      ver[c].push_back(x);
    }
    long long res = 0;
    int pref = n;
    int cnt = 0;
    multiset<int> s;
    for (int i = n - 1; i >= 0; i--) {
      pref -= ver[i].size();
      int nd = i - pref;
      for (auto x : ver[i]) s.insert(x);
      while (cnt < nd) {
        auto it = s.begin();
        res += *it;
        s.erase(it);
        cnt++;
      }
    }
    cout << res << endl;
  }
  return 0;
}
