#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int q;
int n;
int cnt0[MAX_N], cnt1[MAX_N], sa[MAX_N];
priority_queue<int> que;
bool cmp(int a, int b) { return cnt0[a] + cnt1[a] > cnt0[b] + cnt1[b]; }
int main() {
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      if (y)
        cnt0[x]++;
      else
        cnt1[x]++;
      sa[i] = i;
    }
    while (que.size()) que.pop();
    sort(sa + 1, sa + n + 1, cmp);
    int now = 1, ans = 0, num = 0;
    for (int i = n; i >= 1; i--) {
      while (cnt0[sa[now]] + cnt1[sa[now]] >= i) {
        que.push(cnt0[sa[now]]);
        now++;
      }
      if (que.empty()) continue;
      int v = que.top();
      que.pop();
      ans += i;
      num += min(v, i);
    }
    printf("%d %d\n", ans, num);
    for (int i = 1; i <= n; i++) cnt0[i] = cnt1[i] = 0;
  }
  return 0;
}
