#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int T, n;
vector<int> a[maxn];
multiset<int> S;
long long ans;
inline int read() {
  int res = 0, f_f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f_f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    res = (res << 3) + (res << 1) + ch - '0', ch = getchar();
  return res * f_f;
}
int main() {
  T = read();
  while (T--) {
    n = read(), ans = 0;
    for (int i = 1; i <= n; i++) a[i].clear();
    for (int i = 1; i <= n; i++) {
      int mi = read(), pi = read();
      a[mi].push_back(pi);
    }
    S.clear();
    for (int i = n; i; i--) {
      for (int j = 0; j < a[i].size(); j++) S.insert(a[i][j]);
      while (n - S.size() < i) ans += *S.begin(), S.erase(S.begin());
    }
    printf("%lld\n", ans);
  }
  return 0;
}
