#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, n, i, j, *max, sum, k;
  cin >> q;
  while (q--) {
    sum = 0;
    cin >> n;
    int ct[n + 1], a[n + 1];
    bool done[n + 1];
    for (i = 0; i <= n; i++) {
      ct[i] = 0;
      done[i] = false;
    }
    for (i = 1; i <= n; i++) {
      cin >> a[i];
      ++ct[a[i]];
    }
    sort(ct, ct + n + 1);
    max = std::max_element(ct, ct + n + 1);
    k = n;
    for (i = *max; i >= 1; i--) {
      if (ct[k] >= i) {
        sum += i;
        k--;
        continue;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
