#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
stringstream output;
inline void eOP();
inline void solve() {
  int n;
  cin >> n;
  vector<vector<int>> price(n);
  for (int _ = 0; _ < n; ++_) {
    int m, p;
    cin >> m >> p;
    price[m].push_back(p);
  }
  priority_queue<int> asdf;
  for (auto& ps : price) {
    for (int p : ps) {
      asdf.push(p);
    }
    if (!asdf.empty()) {
      asdf.pop();
    }
  }
  long long result = 0;
  while (!asdf.empty()) {
    result += asdf.top();
    asdf.pop();
  }
  cout << result << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  size_t t = 1;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    solve();
  }
  eOP();
}
inline void eOP() {
  cout << output.str();
  output.str(string());
  exit(0);
}
