#include <bits/stdc++.h>
using namespace std;
int a[200000 + 5];
int n;
int main() {
  int cas;
  cin >> cas;
  while (cas--) {
    cin >> n;
    for (int i = (0); i <= (n - 1); i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    vector<pair<int, int> > b;
    b.push_back({1, a[0]});
    for (int i = (1); i <= (n - 1); i++) {
      if (b.back().second == a[i])
        b.back().first++;
      else
        b.push_back({1, a[i]});
    }
    sort(b.begin(), b.end());
    int last = b.back().first, sum = 0;
    for (int i = (b.size() - 1); i >= (0); i--) {
      last = min(last, b[i].first);
      sum += last;
      last--;
      if (last == 0) break;
    }
    cout << sum << endl;
  }
  return 0;
}
