#include <bits/stdc++.h>
const int MAXN = 2e5 + 1;
int q, n, a, f, place;
int res[MAXN], ans[2];
std::priority_queue<int> sum;
struct node {
  int num, cnt;
  node(int nn = 0, int cc = 0) {
    num = nn;
    cnt = cc;
  }
} candy[MAXN];
bool cmp(const node &x, const node &y) { return x.num > y.num; }
void ini(int key) {
  for (int i = 1; i <= key; ++i) {
    candy[i].num = 0;
    candy[i].cnt = 0;
    res[i] = 0;
  }
  ans[0] = ans[1] = 0;
  while (!sum.empty()) {
    sum.pop();
  }
  place = 1;
}
int main() {
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    ini(n);
    for (int i = 1; i <= n; ++i) {
      scanf("%d%d", &a, &f);
      ++candy[a].num;
      if (f == 1) {
        ++candy[a].cnt;
      }
    }
    std::sort(candy + 1, candy + n + 1, cmp);
    res[0] = candy[1].num + 1;
    for (int i = 1; (i <= n) && (candy[i].num != 0) && (res[i - 1] != 0); ++i) {
      res[i] = std::min(res[i - 1] - 1, candy[i].num);
      ans[0] += res[i];
    }
    for (int i = 1; (i <= n) && (res[i] != 0); ++i) {
      for (; (place <= n) && (candy[place].num >= res[i]); ++place) {
        sum.push(candy[place].cnt);
      }
      ans[1] += std::min(sum.top(), res[i]);
      sum.pop();
    }
    printf("%d %d\n", ans[0], ans[1]);
  }
  return 0;
}
