#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
const long long INF = 1e18;
const long long M = 1e9 + 7;
signed main() {
  long long n, q;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> q;
  const long long maxn = 3e5;
  static long long c[maxn];
  while (q--) {
    cin >> n;
    fill(c + 1, c + n + 1, 0);
    for (long long i = 0; i < n; ++i) {
      long long x;
      cin >> x;
      c[x]++;
    }
    sort(c + 1, c + n + 1);
    long long res = 0, cur = c[n];
    for (long long i = n; i > 0; --i) {
      while (cur > 0 && cur > c[i]) {
        --cur;
      }
      if (cur > 0 && c[i] >= cur) {
        res += cur--;
      }
      if (cur == 0) break;
    }
    cout << res << endl;
  }
  return 0;
}
