#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 44;
const int X[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int Y[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int mod = 1e9 + 7;
const int INF = INT_MAX;
int sum(int a) {
  int res = 0;
  while (a) {
    res += a % 10;
    a /= 10;
  }
  return res;
}
long long a[N], cnt[N];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    for (int i = int(0); i < int(n); i++) cnt[i] = 0;
    for (int i = int(0); i < int(n); i++) {
      cin >> a[i];
      a[i]--;
      cnt[a[i]]++;
    }
    sort(cnt, cnt + n);
    reverse(cnt, cnt + n);
    long long d = n + 1;
    long long ans = 0;
    for (int i = int(0); i < int(n); i++) {
      long long k = max(0ll, min(d - 1, cnt[i]));
      ans += k;
      d = k;
    }
    cout << ans << "\n";
  }
}
