#include <bits/stdc++.h>
using namespace std;
long long solver() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  map<int, int> b;
  for (int i = 0; i < n; i++) {
    if (b.find(a[i]) == b.end()) {
      b[a[i]] = 1;
    } else {
      b[a[i]]++;
    }
  }
  vector<int> c;
  for (auto i : b) {
    c.push_back(i.second);
  }
  sort(c.begin(), c.end());
  n = c.size();
  int prev = c[n - 1];
  long long cnt = prev;
  for (int i = n - 2; i >= 0; --i) {
    prev = max(0, min(c[i], prev - 1));
    cnt += prev;
  }
  return cnt;
}
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cout << solver() << endl;
  }
  return 0;
}
