#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  int q;
  cin >> q;
  while (q > 0) {
    q--;
    int n;
    cin >> n;
    vector<int> v[n + 1];
    for (int i = 0; i < n; i++) {
      int l, r;
      cin >> l >> r;
      v[l].push_back(r);
    }
    int sum = n, ans = 0, kol = 0;
    multiset<int> st;
    for (int i = n; i >= 1; i--)
      if (v[i].size() > 0) {
        sum -= v[i].size();
        for (int j = 0; j < v[i].size(); j++) st.insert(v[i][j]);
        while (sum + kol < i) {
          ans += *(st.begin());
          st.erase(st.begin());
          kol++;
        }
      }
    cout << ans << endl;
  }
}
