#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, temp;
    cin >> n;
    vector<int> a(n + 1, 0);
    for (int i = 1; i <= n; i++) {
      cin >> temp;
      a[temp]++;
    }
    sort(a.begin(), a.end(), greater<int>());
    int sum = a[0];
    int max = a[0] - 1;
    for (int i = 1; i <= n; i++) {
      if (a[i] == 0 || max == 0) break;
      if (a[i] >= max) {
        sum += max;
        max--;
      } else {
        sum += a[i];
        max = a[i] - 1;
      }
    }
    cout << sum << endl;
  }
}
