#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const double eps = 1e-7;
const double PI = 3.1415926;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int q;
  scanf("%d", &q);
  while (q--) {
    int n;
    scanf("%d", &n);
    vector<vector<int>> v(n, vector<int>());
    for (int i = 0; i < n; i++) {
      int x, s;
      scanf("%d %d", &x, &s);
      v[x].push_back(s);
    }
    multiset<int> q;
    long long ans = 0;
    int pref = n, cnt = 0;
    for (int i = n - 1; i >= 0; i--) {
      pref -= (int)v[i].size();
      int need = i - pref;
      for (auto x : v[i]) {
        q.insert(x);
      }
      while (cnt < need) {
        ++cnt;
        ans += *q.begin();
        q.erase(q.begin());
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
