#include <bits/stdc++.h>
using namespace std;
long long n, q, i, s;
int main() {
  cin >> q;
  while (q--) {
    cin >> n;
    s = 0;
    vector<int> a(n);
    vector<int> b(n + 1);
    for (auto &i : a) cin >> i, b[i]++;
    sort(b.begin(), b.end());
    for (i = n; i > 0 && b.size() > 0; i--) {
      auto k = lower_bound(b.begin(), b.end(), i);
      if (k != b.end() && *k >= i) {
        s += i;
        b.erase(k);
      }
    }
    cout << s << endl;
  }
}
