#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000007;
const int MOD = 1000000007;
const int nmax = 201234;
int n;
pair<int, int> g[nmax], res;
priority_queue<int> q2;
int main() {
  int q;
  scanf("%d", &q);
  while (q-- > 0) {
    scanf("%d", &n);
    for (int a, f, i = 0; i < n; ++i) {
      scanf("%d", &a);
      f = 0;
      --a;
      g[a].first += 1;
      g[a].second += f;
    }
    sort(g, g + n);
    for (int i = n, j = n - 1; i > 0; --i) {
      while (j >= 0 && g[j].first >= i) q2.push(g[j--].second);
      if (!q2.empty()) {
        res.first += i;
        res.second += min(i, q2.top());
        q2.pop();
      }
    }
    printf("%d\n", res.first);
    res = {0, 0};
    memset(g, 0, sizeof(int) * 2 * n);
    while (!q2.empty()) q2.pop();
  }
  return 0;
}
