#include <bits/stdc++.h>
using namespace std;
long long q, n, t1, t2, t[200005];
int main() {
  cin >> q;
  while (q--) {
    cin >> n;
    for (int i = 0; i < n + 1; ++i) t[i] = 0;
    for (int i = 0; i < n; ++i) cin >> t1, ++t[t1];
    sort(t, t + n + 1);
    reverse(t, t + n + 1);
    long long i = 1, tp = t[0], u = t[0];
    while (tp > 0 && i < n + 1) {
      tp = min(tp - 1, t[i]);
      u += tp;
      ++i;
    }
    cout << u << endl;
  }
  return 0;
}
