#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    int a[n];
    int b[n + 1];
    set<int> s;
    for (int i = 0; i < n + 1; i++) b[i] = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      b[a[i]]++;
    }
    sort(b, b + n + 1, greater<int>());
    int max = b[0];
    for (int i = 0; i <= n; i++) {
      if (b[i] == 0) break;
      if (s.find(b[i]) != s.end()) {
        s.insert(max - 1);
        max = max - 1;
        if (max == 0) break;
      } else {
        s.insert(b[i]);
        max = b[i];
      }
    }
    int sum = 0;
    for (auto i = s.begin(); i != s.end(); i++) sum += *(i);
    cout << sum << endl;
  }
}
