#include <bits/stdc++.h>
using namespace std;
int solve() {
  int n;
  cin >> n;
  vector<int> candy;
  for (int i = 0; i < n; i++) {
    candy.push_back(0);
  }
  for (int i = 0; i < n; i++) {
    int j;
    cin >> j;
    candy[j - 1]++;
  }
  sort(candy.begin(), candy.end());
  int ret = 0;
  int last = candy[n - 1] + 1;
  for (int i = n - 1; i >= 0; i--) {
    if (candy[i] == 0) break;
    if (last > candy[i]) {
      ret += candy[i];
      last = candy[i];
    } else {
      ret += min(last - 1, candy[i]);
      last = min(last - 1, candy[i]);
    }
    if (last == 0) break;
  }
  return ret;
}
int main() {
  int q;
  cin >> q;
  for (int t = 0; t < q; t++) {
    cout << solve() << "\n";
  }
  return 0;
}
