#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int MAXN = 2e5 + 100;
void solve() {
  int n;
  cin >> n;
  vector<int> hash(n + 1);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    hash[x]++;
  }
  sort(hash.rbegin(), hash.rend());
  int res, ans;
  res = ans = hash[0];
  for (int i = 1; i < n + 1; i++) {
    if (res == 0) break;
    if (hash[i] >= res) {
      ans += res - 1;
      res -= 1;
    } else {
      ans += hash[i];
      res = hash[i];
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
