#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
const int N = 200200;
int a[N];
int f[N];
pair<int, int> Cnt[N];
void TestCase() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", a + i, f + i);
    a[i]--;
    Cnt[a[i]].first++;
    if (f[i]) Cnt[a[i]].second++;
  }
  sort(Cnt, Cnt + n, greater<pair<int, int> >());
  pair<int, int> Ans = pair<int, int>(0, 0);
  int Can = n;
  int Last = 0;
  priority_queue<pair<int, int> > q;
  do {
    if (!q.empty()) {
      pair<int, int> p = q.top();
      q.pop();
      int first = max(0, min(p.second, Can));
      int second = min(first, p.first);
      Ans.first += first;
      Ans.second += second;
      Can = max(0, first - 1);
    }
    while (Last < n && (q.empty() || Cnt[Last].first >= Can)) {
      q.push(pair<int, int>(Cnt[Last].second, Cnt[Last].first));
      Last++;
    }
  } while (!q.empty());
  printf("%d %d\n", Ans.first, Ans.second);
  for (int i = 0; i < n; i++) Cnt[i] = pair<int, int>(0, 0);
}
int main() {
  int T;
  cin >> T;
  while (T--) TestCase();
}
