#include <bits/stdc++.h>
using namespace std;
template <typename T>
void _dbg(char const *s, const T &t) {
  cout << s << "=" << t << endl;
}
template <typename T, typename... TA>
void _dbg(char const *s, const T &t, const TA &...ta) {
  while (*s != ',') cout << *s++;
  cout << "=" << t << ",";
  _dbg(s + 1, ta...);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <typename T>
ostream &print_iterable(ostream &os, T beg, T end) {
  os << '[';
  for (auto it = beg; it != end; ++it) {
    if (it != beg) os << ", ";
    os << *it;
  }
  return os << ']';
}
template <typename T, size_t N>
ostream &operator<<(ostream &os, const array<T, N> &x) {
  return print_iterable(os, x.begin(), x.end());
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &x) {
  return print_iterable(os, x.begin(), x.end());
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &x) {
  return print_iterable(os, x.begin(), x.end());
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &x) {
  return print_iterable(os, x.begin(), x.end());
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &x) {
  return print_iterable(os, x.begin(), x.end());
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &x) {
  return print_iterable(os, x.begin(), x.end());
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const unordered_map<T1, T2> &x) {
  return print_iterable(os, x.begin(), x.end());
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
const int inf = 1e9 + 9;
void Solve() {
  int n, a;
  unordered_map<int, int> mp;
  vector<int> types;
  scanf("%d", &n);
  while (n--) {
    scanf("%d", &a);
    ++mp[a];
  }
  for (auto &x : mp) {
    types.push_back(x.second);
  }
  sort((types).begin(), (types).end(), greater<int>());
  ;
  int sum = 0, last = inf;
  for (auto &x : types) {
    last = min(last - 1, x);
    sum += max(0, last);
  }
  printf("%d\n", sum);
}
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    Solve();
  }
  return 0;
}
