#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ll q;
  cin >> q;
  while (q--) {
    ll n;
    cin >> n;
    vector<ll> cnt(n + 1);
    vector<ll> arr(n);
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
      cnt[arr[i]]++;
    }
    sort(cnt.begin(), cnt.end());
    ll M = 1e17;
    ll sum = 0;
    for (int i = n; i >= 0; i--) {
      if (M == 0) {
        break;
      }
      if (cnt[i] > 0) {
        if (cnt[i] >= M) {
          cnt[i] = max(0ll, M - 1);
        }
        M = cnt[i];
        sum += M;
      } else {
        break;
      }
    }
    cout << sum << '\n';
  }
  return 0;
}
