#include <bits/stdc++.h>
using namespace std;
int A[200005], F[200005];
vector<int> S[200005];
void pro() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) A[i] = F[i] = 0, S[i].clear();
  for (int i = 0; i < n; i++) {
    int a, f;
    cin >> a >> f;
    A[a]++;
    F[a] += f;
  }
  for (int i = 1; i <= n; i++) {
    S[A[i]].push_back(F[i]);
  }
  priority_queue<int> pq;
  int acnt = 0, fcnt = 0;
  for (int i = n; i > 0; i--) {
    for (int j : S[i]) pq.push(j);
    if (pq.empty()) continue;
    acnt += i;
    fcnt += min(i, pq.top());
    pq.pop();
  }
  cout << acnt << ' ' << fcnt << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) pro();
  return 0;
}
