#include <bits/stdc++.h>
using namespace std;
int tc;
void qa();
void qb();
void qc();
void qd();
void qe();
int main() {
  cin >> tc;
  for (int h = 0; h < tc; ++h) {
    qe();
  }
  return 0;
}
void qa() {
  string s;
  cin >> s;
  s += ' ';
  int alphabet[26] = {
      0,
  };
  int continuous = 1;
  for (int i = 1; i < s.length() + 1; ++i) {
    if (s[i - 1] == s[i]) {
      continuous++;
    } else {
      if (continuous % 2 == 1) {
        alphabet[s[i - 1] - 'a'] = 1;
        continuous = 1;
      } else {
        continuous = 1;
      }
    }
  }
  for (int i = 0; i < 26; ++i) {
    if (alphabet[i] == 1) {
      printf("%c", i + 'a');
    }
  }
  cout << '\n';
}
void qb() {
  int n;
  cin >> n;
  vector<string> strings(n);
  bool odd = false;
  int count[2] = {
      0,
  };
  for (auto &str : strings) {
    cin >> str;
    if (str.size() % 2 != 0) odd = true;
    for (char c : str) {
      if (c == '0')
        count[0]++;
      else
        count[1]++;
    }
  }
  if (odd)
    cout << n;
  else if (count[0] % 2 == 0)
    cout << n;
  else
    cout << n - 1;
  cout << '\n';
}
void qc() {
  string a;
  cin >> a;
  string answer = "";
  queue<char> even, odd;
  for (int i = 0; i < a.length(); ++i) {
    if (a[i] == '0' || (a[i] - '0') % 2 == 0)
      even.push(a[i]);
    else
      odd.push(a[i]);
  }
  while (!even.empty() || !odd.empty()) {
    if (odd.empty() || (!even.empty() && even.front() < odd.front())) {
      answer += even.front();
      even.pop();
    } else {
      answer += odd.front();
      odd.pop();
    }
  }
  cout << answer << '\n';
}
typedef struct _salary {
  int minimum;
  int maximum;
  bool operator<(const struct _salary &other) const {
    return minimum < other.minimum;
  }
} Salary;
vector<Salary> salaries;
bool possible(int mid, int n, long long s, int cnt) {
  long long sum = 0;
  int count = 0;
  for (auto &sal : salaries) {
    sum += sal.minimum;
  }
  for (int i = n - 1; i >= 0 && count < cnt; --i) {
    if (salaries[i].maximum >= mid) {
      sum += max(mid - salaries[i].minimum, 0);
      count++;
    }
  }
  return sum <= s && count == cnt;
}
void qd() {
  int n, cnt;
  long long s;
  cin >> n >> s;
  salaries.resize(n);
  for (auto &sal : salaries) {
    cin >> sal.minimum >> sal.maximum;
  }
  sort(salaries.begin(), salaries.end());
  cnt = (n + 1) / 2;
  int start = 0, end = 1e9;
  while (start < end) {
    int mid = (start + end + 1) / 2;
    if (possible(mid, n, s, cnt)) {
      start = mid;
    } else {
      end = mid - 1;
    }
  }
  cout << start << '\n';
}
typedef struct _voter {
  int m;
  long long p;
  bool operator<(const struct _voter &other) const {
    if (m == other.m) return p < other.p;
    return m < other.m;
  }
} Voter;
void qe() {
  int n, cnt = 0;
  long long answer = 0;
  cin >> n;
  vector<Voter> voters;
  map<int, long long> minimum;
  for (int i = 0; i < n; ++i) {
    Voter tmp;
    cin >> tmp.m >> tmp.p;
    voters.push_back(tmp);
  }
  sort(voters.begin(), voters.end());
  for (int i = 0; i < n; ++i) {
    minimum.insert(make_pair(voters[i].m, voters[i].p));
  }
  auto riter = minimum.rbegin();
  int fpivot = 0;
  int last = n <= riter->first ? n - 1 : riter->first - 1;
  if (last < 0) last = 0;
  long long next = minimum[voters[last].m];
  while (cnt != n) {
    for (int i = fpivot; i < n; ++i) {
      if (cnt >= voters[i].m) {
        cnt++;
        fpivot = i + 1;
      }
    }
    if (fpivot == n) break;
    answer += next;
    cnt++;
    riter++;
    last = n <= riter->first - cnt ? n - 1 : riter->first - 1 - cnt;
    if (last < 0) last = 0;
    next = minimum[voters[last].m];
  }
  cout << answer << '\n';
}
