#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
vector<int> V[200005];
int n, t;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; i++) V[i].clear();
    for (int i = 0; i < n; i++) {
      int p, x;
      cin >> x >> p;
      V[x].push_back(p);
    }
    multiset<int> st;
    int pre = n, cnt = 0, ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      pre -= (int)(V[i]).size();
      for (auto t : V[i]) st.insert(t);
      int tmp = i - pre;
      while (cnt < tmp) {
        cnt++;
        ans += *st.begin();
        st.erase(st.begin());
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
