#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, i, q, j = 0, k = 0, ans = 0, m = 0, y, x = 0;
  cin >> q;
  while (q--) {
    cin >> n;
    ans = 0, m = 0;
    vector<pair<long long, long long> > v(n, make_pair(0, 0));
    for (i = 0; i < n; i++) {
      cin >> x >> y;
      x--;
      v[x].first++;
      v[x].second += y;
    }
    sort((v).begin(), (v).end());
    priority_queue<long long> Q;
    j = n - 1;
    for (int i = n; i > 0; i--) {
      while (j >= 0 && v[j].first >= i) {
        Q.push(v[j].second);
        j--;
      }
      if (!Q.empty()) {
        ans += i;
        m += min(i, (int)Q.top());
        Q.pop();
      }
    }
    cout << ans << " " << m << endl;
  }
}
