#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  int q;
  cin >> q;
  for (int qq = 0; qq < q; qq++) {
    int n;
    cin >> n;
    map<int, int> cnt;
    for (int i = 0; i < n; i++) {
      int t;
      cin >> t;
      cnt[t]++;
    }
    vector<int> tcnt;
    for (map<int, int>::iterator it = cnt.begin(); it != cnt.end(); it++) {
      tcnt.push_back(it->second);
    }
    sort(tcnt.begin(), tcnt.end());
    int ans = 0;
    int last = INT_MAX;
    for (int i = tcnt.size() - 1; i >= 0; i--) {
      int select = min(last - 1, tcnt[i]);
      if (select <= 0) break;
      ans += select;
      last = select;
    }
    cout << ans << endl;
  }
  return 0;
}
