#include <bits/stdc++.h>
using namespace std;
int a[210000], cnt[210000];
multiset<int> s;
int main() {
  int q;
  cin >> q;
  while (q--) {
    s.clear();
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      cnt[a[i]]++;
    }
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
      if (cnt[a[i]]) s.insert(cnt[a[i]]);
      cnt[a[i]] = 0;
    }
    int ans = 0, k = 1e6;
    while (s.size()) {
      k = min(k, *--s.end());
      s.erase(--s.end());
      ans += k;
      k--;
      if (k == 0) break;
    }
    cout << ans << endl;
  }
  return 0;
}
