#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, n, x;
  cin >> q;
  while (q--) {
    cin >> n;
    int a[n + 1];
    memset(a, 0, sizeof a);
    for (int i = 1; i <= n; i++) {
      cin >> x;
      a[x]++;
    }
    vector<int> v;
    for (int i = 1; i <= n; i++) {
      if (a[i] > 0) v.push_back(a[i]);
    }
    sort((v).begin(), (v).end());
    reverse((v).begin(), (v).end());
    int ans = 0, res = 1000000007;
    for (int i = 0; i < v.size(); i++) {
      if (v[i] < res) {
        ans += v[i];
        res = v[i];
      } else if (res > 0) {
        ans += res - 1;
        res--;
      } else
        break;
    }
    cout << ans << endl;
  }
}
