#include <bits/stdc++.h>
using ll = long long;
int solve();
int main(int argc, char* argv[]) {
  int Q = 1;
  (void)scanf("%d", &Q);
  while (Q--) solve();
  return 0;
}
static int readInt() {
  int x = 0;
  if (1 == scanf("%d", &x)) return x;
  return 0;
}
static ll readLL() {
  ll x = 0;
  if (1 == scanf("%lld", &x)) return x;
  return 0;
}
static const int N = (1 << 18);
int solve() {
  const int n = readInt();
  std::vector<int> cnt(n + 1), cnt_f(n + 1);
  for (int i = 0; i < n; ++i) {
    int a = readInt();
    int f = readInt();
    cnt[a]++;
    cnt_f[a] += f;
  }
  std::vector<std::vector<int> > tp(n + 1);
  for (int i = 1; i <= n; ++i) {
    tp[cnt[i]].push_back(cnt_f[i]);
  }
  std::pair<long long, long long> ans = {0, 0};
  std::set<std::pair<int, int>, std::greater<std::pair<int, int> > > ss;
  int gen = 0;
  for (int i = n; i > 0; --i) {
    for (int fn : tp[i]) ss.insert(std::make_pair(fn, ++gen));
    if (!ss.empty()) {
      auto it = ss.begin();
      std::pair<int, int> greater = *it;
      ss.erase(it);
      int z = greater.first;
      ans.first += i;
      ans.second += std::min(i, z);
    }
  }
  printf("%lld %lld\n", ans.first, ans.second);
  return 0;
}
