#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int inf = 0x3f3f3f3f;
int n;
vector<int> num[N];
int st[N];
struct node {
  int id, x;
  bool operator<(const node &t) const {
    if (x == t.x) return id > t.id;
    return x < t.x;
  }
};
priority_queue<int, vector<int>, greater<int> > q;
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    int i;
    for (i = 0; i <= n; i++) num[i].clear();
    while (q.size()) q.pop();
    for (i = 1; i <= n; i++) {
      int x, y;
      cin >> x >> y;
      num[x].push_back(y);
    }
    long long ans = 0;
    for (i = n - 1; i >= 0; i--) {
      if (!num[i].size()) continue;
      for (auto x : num[i]) q.push(x);
      while (n - i < (int)q.size()) {
        ans += q.top();
        q.pop();
      }
    }
    cout << ans << endl;
  }
}
