#include <bits/stdc++.h>
using std::max;
using std::sort;
const int MAXN = 1E6 + 10;
int cc[MAXN], tong[MAXN];
int w[MAXN];
int doing() {
  int n, v = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    v = max(v, x);
    tong[x]++;
    cc[i] = x;
  }
  int tail = 0;
  for (int i = 1; i <= v; ++i) {
    if (tong[i] != 0) {
      w[++tail] = tong[i];
    }
  }
  for (int i = 1; i <= n; ++i) {
    int x = cc[i];
    tong[x]--;
  }
  sort(w + 1, w + tail + 1);
  int now = tail, bigged = 0;
  int ans = 0;
  for (int i = w[tail]; i >= 1; --i) {
    while (now > 0 && w[now] > i) {
      bigged++;
      now--;
    }
    if (now > 0 && w[now] == i) {
      ans += i;
      now--;
    } else if (bigged > 0) {
      ans += i;
      bigged--;
    }
  }
  printf("%d\n", ans);
  return 0;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t-- > 0) doing();
  return 0;
}
