#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  int n, j, i, x, sum, a[200010];
  scanf("%d", &q);
  while (q--) {
    sum = 0;
    scanf("%d", &n);
    memset(a, 0, sizeof(int) * (n + 1));
    for (i = 1; i <= n; i++) {
      scanf("%d", &x);
      a[x]++;
    }
    sort(a, a + n + 1);
    int min = 99999999;
    for (i = n; i > 0; i--) {
      if (a[i] == 0) {
        break;
      }
      if (a[i] < min) {
        sum += a[i];
        min = a[i];
      } else if (a[i] >= min) {
        min--;
        if (min == 0) {
          break;
        }
        sum += min;
      }
    }
    printf("%d\n", sum);
  }
  return 0;
}
