#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int a[N], cnt[N];
int n;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15);
  int tt;
  cin >> tt;
  while (tt--) {
    cin >> n;
    for (int x = (0); x <= ((n + 1) - 1); x += (+1)) {
      cnt[x] = 0;
    }
    for (int i = (0); i <= ((n)-1); i += (+1)) {
      int x;
      cin >> x;
      cnt[x]++;
    }
    sort(cnt, cnt + n + 1, [&](int x, int y) { return x > y; });
    int gift = 0, prev = n + 1;
    for (int i = (0); i <= ((n + 1) - 1); i += (+1)) {
      int cur = max(0, min(cnt[i], prev - 1));
      gift += cur;
      prev = cur;
    }
    cout << gift << '\n';
  }
  return 0;
}
