#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long q;
  cin >> q;
  while (q--) {
    map<long long, long long> u, v;
    long long n;
    cin >> n;
    for (long long i = 0; i < n; i++) {
      long long a;
      cin >> a;
      u[a]++;
    }
    long long m = -1;
    for (auto i : u) {
      v[i.second]++;
      m = max(m, i.second);
    }
    vector<long long> a(m + 1, 0);
    for (auto i : v) {
      a[i.first] = i.second;
    }
    long long ans = 0;
    for (long long i = m; i >= 1; i--) {
      if (a[i]) {
        ans += i;
        a[i]--;
      }
      a[i - 1] += a[i];
    }
    cout << ans << endl;
  }
  return 0;
}
