#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    vector<pair<int, int> > v;
    for (int i = 1; i <= n; i++) {
      int m, p;
      scanf("%d%d", &m, &p);
      v.emplace_back(m, p);
    }
    sort(v.begin(), v.end(), [](pair<int, int> x, pair<int, int> y) {
      x.second *= -1;
      y.second *= -1;
      return x < y;
    });
    int picked = 0;
    long long cost = 0ll;
    while (picked < n) {
      while (v.front().first > picked) {
        auto it = min_element(v.begin() + 1, v.end(),
                              [](pair<int, int> x, pair<int, int> y) {
                                return x.second < y.second;
                              });
        cost += 1ll * it->second;
        v.erase(it);
        picked++;
      }
      v.erase(v.begin());
      picked++;
      sort(v.begin(), v.end(), [](pair<int, int> x, pair<int, int> y) {
        x.second *= -1;
        y.second *= -1;
        return x < y;
      });
    }
    printf("%lld\n", cost);
  }
  return 0;
}
