#include <bits/stdc++.h>
using namespace std;
int a[200010], b[200010];
vector<int> cnt[200010];
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      int x, y;
      scanf("%d %d", &x, &y);
      a[x]++;
      if (y) b[x]++;
    }
    for (int i = 1; i <= n; i++) {
      if (a[i]) cnt[a[i]].push_back(b[i]);
    }
    multiset<int> st;
    int ans = 0, res = 0;
    for (int i = n; i >= 1; i--) {
      for (int v : cnt[i]) st.insert(v);
      if (st.empty()) continue;
      ans += i;
      int x = *--st.end();
      res += min(x, i);
      st.erase(--st.end());
    }
    printf("%d %d\n", ans, res);
    for (int i = 1; i <= n; i++) {
      a[i] = 0;
      b[i] = 0;
      cnt[i].clear();
    }
  }
}
