#include <bits/stdc++.h>
using namespace std;
long long a[200007];
int main() {
  int q;
  cin >> q;
  while (q--) {
    vector<int> a;
    int n;
    scanf("%d", &n);
    map<int, int> m;
    for (int i = 0; i < n; i++) {
      int t;
      scanf("%d", &t);
      m[t]++;
    }
    map<int, int>::iterator it = m.begin();
    int size = 0;
    while (it != m.end()) {
      a.push_back((*it).second);
      it++;
    }
    sort(a.begin(), a.end());
    long long ans = a[a.size() - 1];
    long long pre = ans;
    for (int i = a.size() - 2; i >= 0; i--) {
      if (a[i] >= pre) {
        pre--;
        ans += pre;
      } else {
        pre = a[i];
        ans += a[i];
      }
      if (pre == 1 || pre == 0) {
        break;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
