#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
istream &operator>>(istream &in, pair<T, U> &rhs) {
  in >> rhs.first;
  in >> rhs.second;
  return in;
}
template <class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &rhs) {
  out << rhs.first;
  out << " ";
  out << rhs.second;
  return out;
}
template <class T, class U>
pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) {
  return pair<T, U>(a.first + b.first, a.second + b.second);
}
template <class T, class U>
pair<T, U> operator-(pair<T, U> &a, pair<T, U> &b) {
  return pair<T, U>(a.first - b.first, a.second - b.second);
}
template <class T>
istream &operator>>(istream &in, vector<T> &a) {
  for (auto &i : a) cin >> i;
  return in;
}
template <class T>
ostream &operator<<(ostream &out, const vector<T> &a) {
  for (auto &i : a) cout << i << " ";
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const set<T> &a) {
  for (auto &i : a) cout << i << " ";
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const unordered_set<T> &a) {
  for (auto &i : a) cout << i << " ";
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const multiset<T> &a) {
  for (auto &i : a) cout << i << " ";
  return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const map<T, U> &a) {
  for (auto &i : a) cout << "(" << i.first << ", " << i.second << "(\n";
  return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const unordered_map<T, U> &a) {
  for (auto &i : a) cout << "(" << i.first << ", " << i.second << "(\n";
  return out;
}
void judge() {}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  ;
  long long t = 1;
  judge();
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<int> g[n];
    for (long long i = 0; i < n; i++) {
      long long a, b;
      cin >> a >> b;
      g[a].push_back(b);
    }
    multiset<long long> s;
    long long ans = 0;
    long long cnt = 0;
    int pre = n;
    for (int i = n - 1; i >= 0; i--) {
      pre -= g[i].size();
      int req = i - pre;
      for (auto j : g[i]) s.insert(j);
      while (cnt < req) {
        cnt++;
        ans += *s.begin();
        s.erase(s.begin());
      }
    }
    cout << ans << endl;
  }
}
