#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long int INFLL = 0x3f3f3f3f3f3f3f3f;
const int maxn = 300010;
int T;
int n;
vector<int> g[maxn];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> T;
  while (T--) {
    priority_queue<int, vector<int>, greater<int>> heap;
    cin >> n;
    for (register int i = (0); i <= (n); i++) {
      g[i].clear();
    }
    for (register int i = (1); i <= (n); i++) {
      int a, b;
      cin >> a >> b;
      g[a].push_back(b);
    }
    long long int ans = 0;
    for (register int i = (n - 1); i >= (0); i--) {
      for (register int j = (0); j <= ((int)g[i].size() - 1); j++) {
        heap.push(g[i][j]);
      }
      while (heap.size() > n - i) {
        ans += heap.top();
        heap.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
