#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> vec[N];
void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i <= n; i++) vec[i].clear();
  for (int i = 1; i <= n; i++) {
    int m, p;
    scanf("%d%d", &m, &p);
    vec[m].push_back(p);
  }
  long long ans = 0;
  int cnt = 0;
  multiset<int> st;
  for (int i = n - 1; i >= 1; i--) {
    cnt += (int)vec[i].size();
    int need = max(0, cnt - (n - i));
    for (auto u : vec[i]) st.insert(u);
    cnt -= need;
    while (need--) {
      ans += *st.begin();
      st.erase(st.begin());
    }
  }
  printf("%lld\n", ans);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}
