#include <bits/stdc++.h>
using namespace std;
int t, n, m, p;
vector<int> g[200005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; ++i) g[i].resize(0);
    for (int i = 1; i <= n; ++i) {
      cin >> m >> p;
      g[m].push_back(p);
    }
    multiset<int> st;
    int cnt = 0, cur = n;
    long long res = 0;
    for (int i = n - 1; i >= 0; --i) {
      cur -= g[i].size();
      int need = i - cur;
      for (int x : g[i]) st.insert(x);
      while (cnt < need) {
        ++cnt;
        res += *st.begin();
        st.erase(st.begin());
      }
    }
    cout << res << '\n';
  }
}
