#include <bits/stdc++.h>
char _;
using namespace std;
void PRINT(int x, int y) {
  for (int i = y - 1; i >= 0; i--) {
    cout << ((0u == (x & (1 << i))) ? 0u : 1u);
  }
  cout << endl;
}
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> c(n);
    for (int x = 0; x < (n); x++) {
      cin >> c[x];
    }
    sort((c).begin(), (c).end());
    vector<int> cnt;
    for (int x = 0; x < (n); x++) {
      if (x == 0 || c[x] != c[x - 1]) {
        cnt.push_back(1);
      } else {
        cnt[cnt.size() - 1]++;
      }
    }
    sort((cnt).begin(), (cnt).end());
    reverse((cnt).begin(), (cnt).end());
    long long ans = 0;
    for (int x = 0; x < ((int)cnt.size()); x++) {
      if (x > 0 && cnt[x] >= cnt[x - 1]) {
        cnt[x] = cnt[x - 1] - 1;
      }
      ans += max(0, cnt[x]);
    }
    cout << ans << "\n";
  }
}
