#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 10;
pair<int, int> a[M];
multiset<int> st;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &a[i].first, &a[i].second);
    }
    sort(a + 1, a + n + 1);
    int tmp = 0;
    long long ans = 0ll;
    st.clear();
    for (int i = n; i >= 1; i--) {
      st.insert(a[i].second);
      while (tmp < a[i].first - i + 1) {
        tmp++;
        multiset<int>::iterator it = st.begin();
        ans += (*it);
        st.erase(it);
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
