#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<int> cnt(n + 1);
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      cnt[x]++;
    }
    sort(cnt.rbegin(), cnt.rend());
    int ans = cnt[0], prev = cnt[0];
    for (int i = 1; i <= n && prev > 0; i++) {
      ans += min(prev - 1, cnt[i]);
      prev = min(prev - 1, cnt[i]);
    }
    cout << ans << "\n";
  }
  return 0;
}
