#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 1;
const int32_t MOD = 1e9 + 7;
const int32_t MM = 998244353;
const long long N = 1e6 + 5;
void solve() {
  long long n;
  cin >> n;
  map<long long, long long> m;
  vector<long long> v;
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    m[x]++;
  }
  for (auto i : m) {
    v.push_back(i.second);
  }
  sort((v).begin(), (v).end(), greater<long long>());
  long long ans = v[0], curr = v[0];
  for (long long i = 1; i < (long long)((v).size()); i++) {
    curr = min(v[i], max(0LL, curr - 1));
    ans += curr;
  }
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
