#include <bits/stdc++.h>
using namespace std;
long long numbit(long long x) { return (1LL << x) - 1LL; }
long long getbit(long long x, long long i) { return (x >> i) & 1LL; }
long long onbit(long long x, long long i) { return x | (1LL << i); }
long long offbit(long long x, long long i) { return x & (~(1LL << i)); }
int newMaskNULL(int mask, int m) { return (mask << 1) % (1 << m); }
int newMask(int mask, int m) { return ((mask << 1) % (1 << m)) | 1; }
const long long Inf64 = 1e18;
const long long Inf = 1e9;
const int Nmax = 1e5 + 10;
const long long Mod9 = 1e9 + 9;
const long long Mod7 = 1e9 + 7;
int main() {
  int test;
  cin >> test;
  for (long long h = (1); h <= (test); h++) {
    int n;
    cin >> n;
    int x, y;
    vector<int> m[n + 1];
    for (long long i = (1); i <= (n); i++) {
      cin >> x >> y;
      m[x].push_back(y);
    }
    int pref = n;
    int cnt = 0;
    int res = 0;
    multiset<int> s;
    for (long long i = (n); i >= (0); i--) {
      pref = pref - m[i].size();
      for (auto t : m[i]) s.insert(t);
      while (pref + cnt < i) {
        res += *s.begin();
        s.erase(s.begin());
        cnt++;
      }
    }
    cout << res << endl;
  }
}
