#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    int a;
    int freq[n + 1];
    memset(freq, 0, (n + 1) * sizeof(int));
    set<int> s;
    for (int i = 0; i < n; i++) {
      cin >> a;
      freq[a]++;
    }
    sort(freq, freq + n + 1);
    int last = INT_MAX;
    int result = 0;
    for (int i = n; i + 1; i--) {
      if (last <= 0 || freq[i] <= 0) break;
      if (freq[i] < last) {
        result += freq[i];
        last = freq[i];
      } else {
        if (last <= freq[i]) {
          result += last - 1;
          last--;
        }
      }
    }
    cout << result << "\n";
  }
}
