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