#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<pair<long long int, long long int>> a;
    map<long long int, vector<long long int>> m;
    for (long long int i = 0; i < n; i++) {
      long long int x, y;
      cin >> x >> y;
      m[x].push_back(y);
    }
    for (auto cc : m) {
      sort(cc.second.begin(), cc.second.end());
      reverse(cc.second.begin(), cc.second.end());
      for (long long int i = 0; i < cc.second.size(); i++) {
        a.push_back({cc.first, cc.second[i]});
      }
    }
    long long int x = 0;
    long long int s = 0;
    vector<long long int> b(n, 0);
    for (long long int i = 0; i < n; i++) {
      if (b[i] == 1) continue;
      if (x >= a[i].first) {
        x++;
        continue;
      }
      long long int y = a[i].first - x;
      vector<pair<long long int, long long int>> p;
      for (long long int j = i + 1; j < n; j++) {
        if (b[j] == 0) p.push_back({a[j].second, j});
      }
      sort(p.begin(), p.end());
      for (long long int j = 0; j < y; j++) {
        cout << p[j].second << "    ";
        x++;
        s += p[j].first;
      }
      x++;
    }
    cout << s << endl;
  }
  return 0;
}
