#include <bits/stdc++.h>
using namespace std;
const int Nmax = 100100;
int m[Nmax], t[Nmax], p[Nmax];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int test;
  cin >> test;
  while (test--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> m[i] >> p[i];
    for (int i = 1; i <= n; ++i) t[i] = i;
    sort(t + 1, t + 1 + n, [](int a, int b) { return m[a] > m[b]; });
    int k = n;
    for (int i = 1; i <= n; ++i)
      if (m[t[i]] < k) k--;
    sort(t + 1, t + 1 + n, [](int a, int b) {
      return p[a] > p[b] || (p[a] == p[b] && m[a] < m[b]);
    });
    set<int> s;
    for (int i = k + 1; i <= n; ++i) s.insert(i);
    int res = 0;
    for (int i = 1; i <= n; ++i) {
      auto it = upper_bound(s.begin(), s.end(), m[t[i]]);
      if (it == s.end())
        res += p[t[i]];
      else
        s.erase(it);
    }
    cout << res << endl;
  }
}
