#include <bits/stdc++.h>
using namespace std;
int64_t q, n, i, j, r, x, h[222000];
int main() {
  for (cin >> q; q--;) {
    cin >> n;
    memset(h, 0, (n + 9) * sizeof(n));
    for (i = 0; i < n; i++) cin >> x, h[x]++;
    sort(h, h + n + 1);
    for (i = n, j = h[n], r = 0; j > 0 && i > 0; r += j--, i--)
      j = min(j, h[i]);
    cout << r << endl;
  }
}
