#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const long long N = 2e5 + 7;
long long n, Q;
long long arr[N];
vector<long long> vec[N];
void solve() {
  cin >> n;
  for (long long i = 0; i < n + 1; i++) vec[i].clear();
  for (long long i = 0; i < n; i++) {
    long long m, p;
    cin >> m >> p;
    vec[m].push_back(p);
  }
  long long cnt = 0, cost = 0;
  multiset<long long> st;
  long long pre = n;
  for (long long i = n - 1; i >= 0; i--) {
    pre = pre - vec[i].size();
    long long y = i - (pre + cnt);
    for (auto j : vec[i]) st.insert(j);
    while (y > 0) {
      cost += *(st.begin());
      st.erase(st.begin());
      cnt++;
      y--;
    }
  }
  cout << cost << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  for (long long i = 1; i < t + 1; i++) {
    solve();
  }
}
