#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> cnt(n + 1);
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      cnt[x]++;
    }
    vector<int> freq(n + 1);
    for (int i = 1; i <= n; i++) {
      freq[cnt[i]]++;
    }
    long long sum = 0;
    for (int i = n; i >= 0; i--) {
      if (i < n) freq[i] += freq[i + 1];
      if (freq[i]) {
        sum += i;
        freq[i]--;
      }
    }
    cout << sum << '\n';
  }
  return 0;
}
