#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, countn = 0, temp, temp1;
  cin >> t;
  long long int p;
  while (t--) {
    p = 0;
    countn = 0;
    cin >> n;
    vector<pair<int, long long int>> v(n);
    for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
    sort(v.begin(), v.end());
    for (int i = 0; i < n; i++) {
      if (countn >= v[i].first)
        countn++;
      else {
        temp = i;
        temp1 = i + 1;
        if (v[temp1].first - v[temp].first == 1 && i != 0) {
          temp1++;
          if (temp1 == n) {
            temp1--;
            break;
          }
        }
        countn += temp1 - temp;
        p += v[temp1].second;
        i = temp1;
      }
    }
    cout << p << "\n";
  }
}
