#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long t;
  cin >> t;
  for (long long q = 0; q < t; q++) {
    long long n;
    cin >> n;
    long long mass[n];
    for (long long i = 0; i < n; i++) cin >> mass[i];
    sort(mass, mass + n);
    vector<long long> ans;
    long long c = 1;
    for (long long i = 1; i < n; i++) {
      if (mass[i] == mass[i - 1]) {
        c++;
      } else {
        ans.push_back(c);
        c = 1;
      }
    }
    ans.push_back(c);
    sort(ans.begin(), ans.end());
    reverse(ans.begin(), ans.end());
    set<long long> perm;
    set<long long> res;
    for (long long i = 0; i < ans[0]; i++) {
      perm.insert(i + 1);
    }
    for (long long z = 0; z < ans.size(); z++) {
      long long x = ans[z];
      set<long long>::iterator it = perm.lower_bound(x);
      if (*it == x) {
        res.insert(x);
        perm.erase(x);
        continue;
      }
      if (it == perm.begin()) {
        continue;
      } else {
        --it;
        x = *it;
        perm.erase(x);
        res.insert(x);
      }
    }
    long long flex = 0;
    for (set<long long>::iterator it = res.begin(); it != res.end(); it++) {
      flex += *it;
    }
    cout << flex << endl;
  }
}
