#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
const int mod = 1e9 + 7;
long long int powmod(long long int a, long long int b) {
  long long int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int nxt() {
  int x;
  cin >> x;
  return x;
}
inline void min_self(long long int &a, long long int b) { a = min(a, b); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int tc = nxt();
  while (tc--) {
    int n = nxt();
    vector<int> m(n), p(n);
    for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n)))
      cin >> m[i] >> p[i];
    vector<vector<int> > o(n);
    for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n)))
      o[m[i]].push_back(p[i]);
    multiset<int> ss;
    long long int ans = 0;
    int cnt = 0, suf = 0;
    for (__typeof(0) i = (n) - ((n) > (0)); i != (0) - ((n) > (0));
         i += 1 - 2 * ((n) > (0))) {
      suf += o[i].size();
      int rem = n - suf;
      for (int x : o[i]) ss.insert(x);
      while (rem + cnt < i) {
        assert(ss.size());
        ans += *ss.begin();
        ss.erase(ss.begin());
        cnt++;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
