#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  vector<int> vt;
  ll i, n, tt, k, a, b;
  cin >> tt;
  while (tt--) {
    vt.clear();
    cin >> n;
    int freq[n + 1];
    for (auto &i : freq) i = 0;
    for (i = 0; i < n; i++) {
      cin >> a;
      freq[a]++;
    }
    for (i = 1; i <= n; i++) {
      if (freq[i] != 0) vt.emplace_back(freq[i]);
    }
    sort(vt.begin(), vt.end(), greater<int>());
    int mn = vt[0] - 1, sum = mn + 1;
    for (i = 1; i < vt.size() && mn > 0; i++) {
      mn = min(vt[i], mn);
      sum += mn;
      mn--;
    }
    cout << sum << "\n";
  }
}
