#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long int MOD = 1000000007;
double eps = 1e-12;
void solve();
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
void solve() {
  long long int n;
  cin >> n;
  vector<long long int> v(n);
  unordered_map<long long int, long long int> mp;
  for (long long int i = 0; i < n; i++) {
    cin >> v[i];
    mp[v[i]]++;
  }
  vector<long long int> ans;
  for (auto i : mp) ans.push_back(i.second);
  sort(ans.begin(), ans.end(), greater<long long int>());
  long long int ans1 = ans[0];
  long long int pre = ans[0];
  for (int i = 1; i < ans.size(); i++) {
    if (ans[i] >= pre)
      pre--;
    else
      pre = ans[i];
    if (pre <= 0) break;
    ans1 += pre;
  }
  cout << ans1 << endl;
}
