#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    sort(a.begin(), a.end());
    int count = 1;
    vector<int> ans;
    for (int i = 1; i < n; ++i) {
      if (a[i] != a[i - 1]) {
        ans.push_back(count);
        count = 1;
      } else {
        count++;
        ;
      }
    }
    ans.push_back(count);
    sort(ans.begin(), ans.end());
    if (ans.size() == 1) {
      cout << ans[0] << endl;
    } else {
      int last = ans[ans.size() - 1];
      long long int ans1 = last;
      for (int i = ans.size() - 2; i >= 0; i--) {
        if (last == 0) break;
        if (ans[i] < last) {
          ans1 += ans[i];
          last = ans[i];
        } else {
          ans1 += (last - 1);
          last--;
        }
      }
      cout << ans1 << endl;
    }
  }
}
