#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  cin >> TESTS;
  while (TESTS--) {
    long long n;
    cin >> n;
    long long a[200005];
    for (long long int i = 0; i < n + 1; i++) a[i] = 0;
    for (long long int i = 0; i < n; i++) {
      long long temp;
      cin >> temp;
      a[temp]++;
    }
    long long ans = 0;
    sort(a, a + n + 1);
    long long cur = a[n];
    ans = a[n];
    cur--;
    for (long long i = n - 1; i >= 1; i--) {
      if (a[i] >= cur) {
        if (cur <= 0) break;
        ans += cur;
        cur--;
      } else {
        if (cur <= 0) break;
        cur = a[i];
        ans += cur;
        cur--;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
