#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e6 + 5;
const int NAX = (int)2e5 + 5;
const int MOD = (int)1e9 + 7;
template <class T>
inline void rem(T &n) {
  n = (-MOD <= n && n <= MOD) ? n : n % MOD;
  if (n < 0) n += MOD;
}
pair<int, int> type[NAX];
int chk[NAX];
vector<int> piss[NAX];
void compute() {
  int n;
  cin >> n;
  for (int i = (int)1; i <= (int)n; ++i) {
    type[i].first = 0, type[i].second = 0;
    chk[i] = 0;
    piss[i].clear();
  }
  for (int i = 0; i < (int)n; ++i) {
    int x, f;
    cin >> x >> f;
    ++type[x].first;
    type[x].second += f;
  }
  sort(type + 1, type + n + 1);
  int p = n + 1, res = 0;
  for (int i = n; i && p > 1 && type[i].first; --i) {
    int x = min(type[i].first, p - 1);
    p = x;
    res += p;
    ++chk[p];
  }
  cout << res << " ";
  for (int i = (int)1; i <= (int)n; ++i)
    piss[type[i].first].push_back(type[i].second);
  res = 0;
  priority_queue<int> pq;
  for (int i = n; i; --i) {
    for (int j : piss[i]) pq.push(j);
    if (chk[i]) {
      res += min(pq.top(), i);
      pq.pop();
    }
  }
  cout << res << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while (q--) {
    compute();
  }
  return 0;
}
