#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, n;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> n;
    int a[n], b[n + 1];
    for (int m = 0; m <= n; m++) {
      b[m] = 0;
    }
    for (int j = 0; j < n; j++) {
      cin >> a[j];
      b[a[j]]++;
    }
    sort(b, b + n + 1);
    int y = b[n];
    for (int u = n - 1; u != 0; u--) {
      if (b[u] >= b[u + 1]) {
        b[u] = b[u + 1] - 1;
      }
      y += b[u];
      if (b[u] == 0) {
        break;
      }
    }
    cout << y << endl;
  }
  return 0;
}
