#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 7;
long long cnt[N], n, q;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  cin >> q;
  while (q--) {
    cin >> n;
    for (long long i = 1; i <= n; i++) {
      cnt[i] = 0;
    }
    for (long long i = 1; i <= n; i++) {
      long long x;
      cin >> x;
      cnt[x]++;
    }
    vector<long long> v;
    for (long long i = 1; i <= n; i++)
      if (cnt[i]) v.push_back(cnt[i]);
    sort(v.begin(), v.end(), greater<long long>());
    long long k = v[0] - 1;
    long long sum = v[0];
    for (long long i = 1; i < v.size(); i++) {
      long long x = min(k, v[i]);
      k = x - 1;
      sum += x;
      if (k <= 0) break;
    }
    cout << sum << "\n";
  }
  return 0;
}
