#include <bits/stdc++.h>
template <class T>
T read(std::istream& is) {
  T x;
  return is >> x, x;
}
template <class T>
T read() {
  T x;
  return std::cin >> x, x;
}
const int oo = 0x7fffffff;
const long long OO = 0x7fffffffffffffff;
using namespace std;
struct cmp {
  bool operator()(const pair<int, int>& a, const pair<int, int>& b) {
    return (a.first == b.first ? (a.second > b.second) : (a.first > b.first));
  }
};
void solve() {
  int n;
  cin >> n;
  map<int, pair<int, int> > ct;
  for (int i = 0; i < n; ++i) {
    int a, f;
    cin >> a >> f;
    if (ct.find(a) == ct.end()) ct[a] = {0, 0};
    ct[a].first += (f == 1);
    ct[a].second += (f == 0);
  }
  multiset<pair<int, int>, cmp> V;
  for (auto p : ct) {
    auto x = p.second;
    ;
    V.insert({x.first + x.second, x.first});
  }
  int s = 0, k = oo, good = 0;
  while (V.size() and k) {
    auto p = *V.begin();
    V.erase(V.begin());
    ;
    s += p.first;
    good += p.second;
    k = p.first - 1;
    while (V.size() and k) {
      auto p2 = *V.begin();
      if (p2.first <= k) break;
      V.erase(V.begin());
      p2.first = k;
      p2.second = min(k, p2.second);
      V.insert(p2);
    }
  }
  cout << s << ' ' << good << '\n';
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cout.tie(0);
  ;
  int q;
  cin >> q;
  while (q--) solve();
  return 0;
}
