#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long q, n;
  cin >> q;
  while (q--) {
    cin >> n;
    long long arr[n];
    map<long long, long long> m;
    for (long long i = 0; i < n; i++) {
      cin >> arr[i];
      m[arr[i]]++;
    }
    multiset<long long> st;
    for (auto i : m) st.insert(i.second);
    long long ans = 0, num = 0;
    set<long long> done;
    for (auto i = st.rbegin(); i != st.rend(); ++i) {
      if (done.find(*i) == done.end()) {
        ans += *i;
        done.insert(*i);
      } else {
        num = *i;
        num--;
        while (num > 0) {
          if (done.find(num) == done.end()) {
            ans += num;
            done.insert(num);
            break;
          }
          num--;
        }
        if (num == 0) break;
      }
    }
    cout << ans << endl;
  }
}
