#include <bits/stdc++.h>
using namespace std;
inline void prnt(bool ok) { cout << (ok ? "YES" : "NO"); }
const int ppr[] = {257, 263};
const long long INF = 2e18;
const int inf = 2e9;
const int mod[] = {int(1e9 + 7), int(1e9 + 9)};
const int N = 1e6 + 123;
const long double pi = 3.141592653589793238462643;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
long long q, n, a[N], first[N], c[N], t[N], ta[N];
bool u[N], w[N];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> q;
  while (q--) {
    cin >> n;
    for (long long i = 1; i <= n; i++)
      cin >> a[i] >> first[i], t[a[i]] += first[i], c[a[i]]++;
    multiset<long long> st[n + 5];
    for (long long i = 1; i <= n; i++)
      if (!u[a[i]]) st[c[a[i]]].insert(t[a[i]]), u[a[i]] = 1;
    long long ans = 0, cur = 0;
    multiset<long long> second;
    for (long long i = n; i >= 1; i--) {
      for (auto x : st[i]) second.insert(x);
      if (second.empty()) continue;
      long long x = (*second.rbegin());
      second.erase(second.find(x));
      ans += i;
      cur += min(x, i);
    }
    cout << ans << ' ' << cur << '\n';
    for (int i = 1; i <= n; i++) c[a[i]] = t[a[i]] = u[a[i]] = 0;
  }
  return 0;
}
