#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n, m[maxn], p[maxn], brige[maxn];
vector<int> Type, V[maxn];
vector<int> sav[maxn];
void readf() {
  cin >> n;
  Type.clear();
  for (int i = (1); i <= (n); ++i) {
    cin >> m[i] >> p[i];
    Type.emplace_back(m[i]);
    V[m[i]].clear();
  }
  for (int i = (1); i <= (n); ++i) V[m[i]].emplace_back(p[i]);
  sort(Type.begin(), Type.end());
  (Type).resize(unique((Type).begin(), (Type).end()) - (Type).begin());
}
void solve() {
  int cnt = 0;
  long long ans = 0;
  int sz = 0;
  for (auto it : Type) {
    if (cnt < it) {
      brige[++sz] = it - cnt;
      sav[sz].clear();
      sav[sz].emplace_back(it);
    } else
      sav[sz].emplace_back(it);
    cnt += V[it].size();
  }
  multiset<int> S;
  multiset<int>::iterator pp;
  int deta = 0;
  for (int i = sz; i > 0; --i) {
    for (auto u : sav[i])
      for (auto v : V[u]) S.insert(v);
    if (brige[i] > deta) {
      int c = brige[i] - deta;
      deta = brige[i];
      while (c--) {
        pp = S.begin();
        ans += (*pp);
        S.erase(pp);
      }
    }
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  while (q--) {
    readf();
    solve();
  }
}
