#include <bits/stdc++.h>
using namespace std;
int ar[200001];
pair<int, int> f[200001];
int main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    for (int i = 0; i < (int)(n); ++i) {
      int p;
      cin >> ar[i] >> p;
      f[ar[i]].first++;
      f[ar[i]].second += p;
    }
    sort(f, f + n + 1);
    reverse(f, f + n + 1);
    int mx = f[0].first, j = 1, ans1 = 0, ans2 = 0;
    priority_queue<pair<int, int> > pq;
    pq.push({f[0].second, f[0].first});
    while (mx) {
      while (j <= n && f[j].first >= mx) pq.push({f[j].second, f[j++].first});
      if (!pq.empty()) {
        pair<int, int> cur = pq.top();
        pq.pop();
        ans1 += mx;
        ans2 += min(cur.first, mx);
      }
      mx--;
    }
    cout << ans1 << " " << ans2 << "\n";
    for (int i = 0; i < (int)(n + 1); ++i) f[i].first = f[i].second = 0;
  }
  return 0;
}
