#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int M = 1e5 + 5;
int main() {
  ios_base::sync_with_stdio(false);
  int tt;
  cin >> tt;
  while (tt--) {
    int n, i, j;
    cin >> n;
    vector<long long> v[N];
    for (i = 0; i < n; i++) {
      int m;
      long long p;
      cin >> m >> p;
      v[m].push_back(p);
    }
    int want[N];
    int cnt = 0;
    for (i = 0; i < n; i++) {
      int lu = v[i].size();
      if (lu == 0) continue;
      want[i] = max(0, i - cnt);
      cnt += lu;
    }
    multiset<long long> st;
    long long ans = 0;
    int taken = 0;
    for (i = n - 1; i >= 0; i--) {
      int lu = v[i].size();
      if (lu == 0) continue;
      for (j = 0; j < lu; j++) {
        st.insert(v[i][j]);
      }
      int fu = st.size();
      int req = min(max(0, want[i] - taken), fu);
      for (j = 0; j < req; j++) {
        ans += *(st.begin());
        taken++;
        st.erase(st.begin());
      }
    }
    cout << ans << endl;
  }
  return 0;
}
