#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int q;
  cin >> q;
  for (long long int t = 0; t < q; ++t) {
    long long int n;
    cin >> n;
    long long int a[n];
    set<long long int> s;
    map<long long int, long long int> m;
    for (long long int i = 0; i < n; ++i) {
      cin >> a[i];
      s.insert(a[i]);
      if (m.find(a[i]) != m.end()) {
        ++m[a[i]];
      } else {
        m.insert(pair<long long int, long long int>(a[i], 1));
      }
    }
    vector<long long int> v;
    auto r = s.begin();
    for (r = s.begin(); r != s.end(); ++r) {
      v.push_back(m[*r]);
    }
    sort(v.begin(), v.end());
    long long int zx = v.size();
    long long int max = v[zx - 1], klj = 0;
    long long int sum = v[zx - 1];
    for (long long int i = zx - 2; i >= 0 && klj == 0; --i) {
      if (v[i] >= max) {
        sum += (max - 1);
        --max;
        if (max <= 0) ++klj;
      } else {
        sum += v[i];
        max = v[i];
      }
    }
    cout << sum << endl;
  }
}
