#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const double pi = 3.14159265358979323846264338;
inline long long multi(long long x, long long y) {
  long long tmp =
      (x * y -
       (long long)((long double)x / 998244353 * y + 1.0e-8) * 998244353);
  return tmp < 0 ? tmp + 998244353 : tmp;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    n = n + 1;
    int cnt[n];
    int vis[n];
    memset(cnt, 0, sizeof(cnt));
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i < n; i++) {
      int x;
      scanf("%d", &x);
      cnt[x]++;
    }
    long long ans = 0;
    sort(cnt, cnt + n, greater<int>());
    for (int i = 0; cnt[i] > 0; i++) {
      while (vis[cnt[i]]) {
        cnt[i]--;
      }
      if (cnt[i] > 0) {
        vis[cnt[i]] = 1;
        ans += cnt[i];
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
