#include <bits/stdc++.h>
inline int reads(char s[]) {
  char c;
  int len = 0;
  while ((c = getchar()) != ' ' && c != '\n') s[len++] = c;
  return len;
}
inline int read() {
  char c;
  int sgn = 1;
  do {
    c = getchar();
  } while ((c < '0' || c > '9') && c != '-');
  int ret = 0;
  if (c == '-')
    sgn = -1;
  else
    ret = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  ret *= sgn;
  return ret;
}
inline long long readl() {
  char c;
  int sgn = 1;
  do {
    c = getchar();
  } while ((c < '0' || c > '9') && c != '-');
  long long ret = 0;
  if (c == '-')
    sgn = -1;
  else
    ret = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  ret *= sgn;
  return ret;
}
template <typename T>
inline void write(T x) {
  char F[60];
  if (x < 0) putchar('-'), x = -x;
  F[0] = x % 10 + '0';
  x /= 10;
  int cnt = 1;
  while (x) {
    F[cnt++] = x % 10 + '0';
    x /= 10;
  }
  while (cnt) putchar(F[--cnt]);
  putchar(10);
}
template <typename T>
inline bool read(T &ret) {
  char c;
  ret = 0;
  T sgn = 1;
  do {
    c = getchar();
    if (c == EOF) return false;
  } while ((c < '0' || c > '9') && c != '-');
  if (c == '-')
    sgn = -1;
  else
    ret = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  ret *= sgn;
  return true;
}
template <typename T>
inline void _max(T &a, const T b) {
  if (a < b) a = b;
}
template <typename T>
inline void _min(T &a, const T b) {
  if (a > b) a = b;
}
template <typename T>
inline void _add(T &a, const T b) {
  a = a + b;
}
template <typename T>
inline void _add(T &a, const T b, const T MOD) {
  a = (a + b) % MOD;
}
template <typename T>
inline T _pow(T a, T b, const T MOD) {
  T ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return ans;
}
using namespace std;
const int N = 200010;
const int INF = 1e9;
const long long MOD = 1e9 + 7;
struct P {
  int m;
  long long p;
  bool operator<(const P &rhs) const { return p > rhs.p; }
} ps[N];
bool cmp(const P &a, const P &b) { return a.m > b.m; }
priority_queue<P> q;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    while (!q.empty()) q.pop();
    for (int i = 0; i < n; i++) {
      cin >> ps[i].m >> ps[i].p;
    }
    sort(ps, ps + n, cmp);
    long long res = 0;
    int l = 0, r = 0;
    while (l < n) {
      while (l < n && ps[l].m == ps[r].m) {
        q.push(ps[l]);
        l++;
      }
      while (n - ps[r].m < q.size()) {
        res += q.top().p;
        q.pop();
      }
      r = l;
    }
    cout << res << "\n";
  }
  return 0;
}
