#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
  if (a > b)
    return a;
  else
    return b;
}
long long min(long long a, long long b) {
  if (a < b)
    return a;
  else
    return b;
}
long long fx[] = {1, -1, 0, 0};
long long fy[] = {0, 0, 1, -1};
long long A[300005], B[300005], C[300005], D[200005], visited[300005],
    level[300005];
char CH[1000][1000], ch;
vector<long long> V;
vector<long long> V1;
vector<long long> G;
int main() {
  long long T, N, E, u, v, i, j, k, sum = 0, ck = 0, x, y;
  string S, S1;
  double d1, d2, d3;
  cin >> T;
  for (i = 1; i <= T; i++) {
    cin >> N;
    for (k = 0; k <= N; k++) A[k] = 0;
    long long ans = 0;
    for (j = 1; j <= N; j++) {
      cin >> u;
      A[u]++;
    }
    sort(A + 1, A + N + 1);
    long long mx = N + 1;
    ans = 0;
    for (j = N; j >= 0 && mx > 1; j--) {
      v = min(A[j], mx - 1);
      ans += v;
      mx = v;
    }
    cout << ans << endl;
  }
  return 0;
}
