#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long q;
  cin >> q;
  while (q--) {
    long long n;
    cin >> n;
    long long a[n];
    map<long long, long long> mp;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      mp[a[i]]++;
    }
    vector<long long> v;
    for (auto it = mp.begin(); it != mp.end(); it++) {
      v.push_back(it->second);
    }
    sort(v.begin(), v.end(), greater<long long>());
    long long sum = 0, low = 90;
    for (int i = 0; i < v.size(); i++) {
      if (low == 0) {
        break;
      }
      if (i == 0) {
        sum += v[i];
        low = v[i] - 1;
      } else {
        if (v[i] >= low) {
          sum += low;
          low--;
        } else {
          sum += max(0LL, v[i]);
          low = max(0LL, v[i] - 1);
        }
      }
    }
    cout << sum << '\n';
  }
  return 0;
}
