#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, i, j, k, x, y, t, m, cnt;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<long long int> V;
    vector<long long int> tmp;
    for (i = 1; i <= n; i++) {
      cin >> x;
      V.push_back(x);
    }
    sort(V.begin(), V.end());
    x = V.size();
    set<long long int> S;
    i = 0, j = 0;
    while (i < x) {
      j = i;
      while (j < x) {
        if (V[j] != V[i]) break;
        j++;
      }
      tmp.push_back(j - i);
      i = j;
    }
    sort(tmp.begin(), tmp.end());
    reverse(tmp.begin(), tmp.end());
    x = tmp.size();
    i = 0;
    long long int ans = 0, curr = tmp[0];
    while (i < x) {
      curr = max((long long int)0, min(curr, tmp[i]));
      ans += curr;
      curr--, i++;
    }
    cout << ans << endl;
  }
}
