#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n, i, x, d, temp, sum = 0;
  cin >> n;
  multiset<long long int> am;
  set<long long int> ch;
  for (i = 0; i < n; i++) {
    cin >> x;
    am.insert(x);
    ch.insert(x);
  }
  x = ch.size();
  multiset<long long int>::iterator itr;
  itr = am.begin();
  long long int a[x];
  for (i = 0; i < x; i++) {
    d = am.count(*itr);
    a[i] = d;
    am.erase(*itr);
    itr = am.begin();
  }
  sort(a, a + x, greater<long long int>());
  temp = a[0];
  sum += temp;
  for (i = 1; i < x; i++) {
    if (a[i] == temp) {
      sum += (a[i] - 1);
      temp = a[i] - 1;
    } else if (a[i] > temp) {
      sum += (temp - 1);
      temp = (temp - 1);
    } else {
      sum += a[i];
      temp = a[i];
    }
    if (temp == 1 || temp <= 0) break;
  }
  cout << sum << endl;
}
int main() {
  long long int q;
  cin >> q;
  while (q--) solve();
  return 0;
}
