#include <bits/stdc++.h>
using namespace std;
const int maxv = 2e5 + 5;
int b[maxv];
long long z;
bool cmp(int x, int y) { return x > y; }
int main() {
  ios::sync_with_stdio(false);
  int i, q = 0;
  cin >> q;
  while (q--) {
    int n, a, t = 0;
    cin >> n;
    for (i = 1; i <= n; i++) b[i] = 0;
    for (i = 1; i <= n; i++) cin >> a, b[a]++;
    sort(b + 1, b + n + 1, cmp);
    t = b[1], z = 0;
    for (i = 1; i <= n; i++) {
      if (t <= b[i])
        z += t, t--;
      else
        z += b[i], t = b[i] - 1;
      if (t == 0 || b[i] == 0) break;
    }
    cout << z << endl;
  }
  return 0;
}
