#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline int lb(int x) { return x & -x; }
long long qpow(long long base, int n) {
  if (base == 0) return 0;
  long long ans = 1;
  while (n) {
    if (n & 1) ans *= base;
    n >>= 1;
    base *= base;
  }
  return ans;
}
int t, n;
long long ans;
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    vector<int> ve[n + 1];
    priority_queue<long long, vector<long long>, greater<long long> > pq;
    int m, p;
    for (int i = (1); i <= (n); ++i) {
      cin >> m >> p;
      ve[m].push_back(p);
    }
    int sum = n, cnt = 0;
    ans = 0;
    for (int i = n - 1; i >= 0; --i) {
      sum -= ve[i].size();
      for (auto x : ve[i]) pq.push(x);
      int de = i - sum - cnt;
      for (int i = (1); i <= (de); ++i) {
        ans += pq.top();
        pq.pop();
        ++cnt;
      }
    }
    cout << ans << '\n';
  }
}
