#include <bits/stdc++.h>
using namespace std;
bool comp(pair<int, int> &a, pair<int, int> &b) { return a.second > b.second; }
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    map<int, int> m;
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      m[a[i]]++;
    }
    vector<pair<int, int> > v;
    map<int, int>::iterator itt;
    int prev = -1, sum = 0;
    for (itt = m.begin(); itt != m.end(); itt++) {
      v.push_back(make_pair(itt->first, itt->second));
    }
    sort(v.begin(), v.end(), comp);
    vector<pair<int, int> >::iterator it;
    for (it = v.begin(); it != v.end(); it++) {
      if (prev <= it->second && prev != -1) {
        sum += prev - 1;
        prev = prev - 1;
        if (prev == 0) break;
      } else {
        sum += it->second;
        prev = it->second;
      }
    }
    cout << sum << endl;
  }
}
