#include <bits/stdc++.h>
using namespace std;
int cnt[200005], a[200005];
vector<int> v;
int main() {
  int t, n;
  scanf("%d", &t);
  while (t--) {
    v.clear();
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
      cnt[a[i]]++;
    }
    for (int i = 0; i < n; i++) {
      if (cnt[a[i]] > 0) {
        v.push_back(cnt[a[i]]);
        cnt[a[i]] = 0;
      }
    }
    sort(v.begin(), v.end());
    int sum = 0;
    int last = -1;
    for (int i = v.size() - 1; i >= 0; i--) {
      if (last == -1) {
        sum += v[i];
        last = v[i];
      } else if (v[i] >= last) {
        sum += (last - 1);
        last = last - 1;
      } else {
        sum += v[i];
        last = v[i];
      }
      if (last == 1) {
        break;
      }
    }
    printf("%d\n", sum);
  }
  return 0;
}
