#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long i, j, k, l, n, m, a, b, u;
  cin >> n;
  vector<long long> v(n);
  map<long long, long long> mp;
  long long ans = 0LL;
  priority_queue<pair<long long, long long>> pq;
  for (i = 0; i < n; i++) {
    cin >> v[i];
    mp[v[i]]++;
  }
  unordered_set<long long> st;
  for (auto it : mp) {
    pq.push({it.second, it.first});
  }
  map<long long, long long> mp2;
  while (!pq.empty()) {
    auto p = pq.top();
    pq.pop();
    if (p.first == 0) continue;
    if (st.find(p.first) == st.end()) {
      st.insert(p.first);
      ans += p.first;
    } else {
      pq.push({p.first - 1, p.second});
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) solve();
  return 0;
}
