#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int q, n;
int place;
int res[maxn], ans[2];
priority_queue<int> sum;
struct node {
  int num, cnt;
} candy[maxn];
bool cmp(const node& a, const node& b) { return a.num > b.num; }
void init(int p) {
  for (int i = 1; i <= p; i++) {
    candy[i].num = candy[i].cnt = 0;
    res[i] = 0;
  }
  ans[0] = ans[1] = 0;
  while (!sum.empty()) sum.pop();
  place = 1;
}
int main(int argc, char const* argv[]) {
  ios::sync_with_stdio(false);
  cin >> q;
  while (q--) {
    cin >> n;
    init(n);
    int a, b;
    for (int i = 1; i <= n; i++) {
      cin >> a >> b;
      candy[a].num++;
      if (b == 1) candy[a].cnt++;
    }
    sort(candy + 1, candy + 1 + n, cmp);
    res[0] = candy[1].num + 1;
    for (int i = 1; i <= n && candy[i].num != 0 && res[i - 1] != 0; i++) {
      res[i] = 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] += min(sum.top(), res[i]);
      sum.pop();
    }
    cout << ans[0] << " " << ans[1] << endl;
  }
  return 0;
}
