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