#include <bits/stdc++.h>
using namespace std;
int II() {
  int q;
  scanf("%d", &q);
  return q;
}
long long LII() {
  long long q;
  scanf("%lld", &q);
  return q;
}
struct node {
  int l, r;
  bool operator<(const node &seg) const { return r < seg.r; }
};
void fun(vector<pair<int, int>> v) {
  sort(v.begin(), v.end());
  int last = v.back().first;
  priority_queue<node> q;
  int ans = 0, ans1 = 0;
  while (last > 0) {
    while (v.size() > 0 && v.back().first >= last) {
      q.push({v.back().first, v.back().second});
      v.pop_back();
    }
    if (q.size() == 0)
      last--;
    else {
      auto it = q.top();
      q.pop();
      ans += last;
      ans1 += min(last, it.r);
      last--;
    }
  }
  cout << ans << " " << ans1 << endl;
}
void solve() {
  int N;
  cin >> N;
  unordered_map<int, int> mp, mp1;
  for (int i = 1; i <= N; i++) {
    int a, type;
    cin >> a >> type;
    mp[a]++;
    if (type == 1) mp1[a]++;
  }
  vector<pair<int, int>> v;
  for (auto it : mp) {
    v.push_back({it.second, mp1[it.first]});
  }
  fun(v);
}
int32_t main() {
  int tt;
  cin >> tt;
  int cas = 1;
  while (tt--) {
    solve();
  }
}
