#include <bits/stdc++.h>
using namespace std;
void single_case() {
  int n;
  cin >> n;
  int* A = new int[n];
  memset(A, 0, sizeof(A[0]) * n);
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    A[t - 1]++;
  }
  sort(A, A + n, greater<int>());
  int v = A[0] + 1;
  int ans = 0;
  for (int i = 0; i < n && A[i] && v; i++) {
    if (v > A[i]) {
      ans += A[i];
      v = A[i] - 1;
    } else {
      ans += v;
      v--;
    }
  }
  cout << ans << endl;
}
signed main() {
  int q;
  cin >> q;
  while (q--) single_case();
  return 0;
}
