#include <bits/stdc++.h>
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 998244353LL;
using namespace std;
void solve() {
  long long n;
  scanf("%lld", &n);
  vector<vector<long long>> g(n + 1);
  for (int i = 0; i < n; i++) {
    long long m, p;
    scanf("%lld %lld", &m, &p);
    g[m].push_back(p);
  }
  priority_queue<long long> pq;
  for (int i = 0; i < n; i++) {
    for (auto e : g[i]) pq.push(e);
    if (!pq.empty()) pq.pop();
  }
  long long res = 0;
  while (!pq.empty()) res += pq.top(), pq.pop();
  cout << res << "\n";
}
int main() {
  int kkt;
  scanf("%d", &kkt);
  while (kkt--) {
    solve();
  }
  return 0;
}
