#include <bits/stdc++.h>
using namespace std;
const int max1 = 2e5 + 15;
int q, n, i, temp, s, a[max1], ans;
map<int, int> m;
int main() {
  scanf("%d", &q);
  while (q--) {
    m.clear();
    map<int, int>::iterator it;
    scanf("%d", &n);
    for (i = 1; n >= i; i++) {
      scanf("%d", &temp);
      m[temp]++;
    }
    for (it = m.begin(), s = 1; it != m.end(); ++it) {
      a[s] = it->second;
      s++;
    }
    s--;
    sort(a + 1, a + s + 1);
    ans = 0;
    for (i = s, temp = max1; i >= 1; i--) {
      if (temp == 0) break;
      if (a[i] < temp) {
        ans += a[i];
        temp = a[i];
      } else {
        ans += --temp;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
