#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, n, a, b, k, rS, rI, x, y;
  cin >> q;
  map<int, int> mapa;
  map<int, int>::iterator it;
  vector<int> lista;
  for (int i = 0; i < q; i++) {
    mapa.clear();
    lista.clear();
    cin >> n;
    for (int j = 0; j < n; j++) {
      cin >> a;
      mapa[a]++;
    }
    for (it = mapa.begin(); it != mapa.end(); it++) {
      lista.push_back(it->second);
    }
    sort(lista.begin(), lista.end());
    reverse(lista.begin(), lista.end());
    b = 0;
    for (int j = 0; j < lista.size(); j++) {
      if (j == 0) {
        b += lista[j];
        rS = lista[j];
      } else {
        if (rS > 0) {
          if (lista[j] >= rS) {
            rS--;
            b += rS;
          } else {
            rS = lista[j];
            b += rS;
          }
        }
      }
    }
    cout << b << endl;
  }
  return 0;
}
