#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    long long n;
    cin >> n;
    vector<int> a(n), b(n + 1, 0);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      b[a[i]]++;
    }
    sort(b.rbegin(), b.rend());
    int i = 1, mn = b[0] - 1;
    long long sum = b[0];
    while (b[i] > 0 && mn > 0 && i < n + 1) {
      if (b[i] >= mn) {
        sum += mn;
        mn--;
      } else {
        sum += b[i];
        mn = b[i] - 1;
      }
      i++;
    }
    cout << sum << endl;
  }
  return 0;
}
