#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    std::vector<int> cnt(n + 1);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      cnt[a[i]]++;
    }
    sort(cnt.begin(), cnt.end(), greater<int>());
    int last = cnt[0];
    int sum = last;
    for (int i = 1; i < n + 1; i++) {
      last--;
      if (last > cnt[i]) last = cnt[i];
      sum += last;
      if (last == 0) break;
    }
    cout << sum << endl;
  }
}
