#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 100;
int n, q;
int arr[N], f[N], cnt[N], mal[N];
struct data {
  int cnt, good;
  bool operator<(data o) const {
    if (cnt != o.cnt) return cnt > o.cnt;
    return good > o.good;
  }
};
vector<data> v;
multiset<int> st;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &arr[i], &f[i]);
      cnt[arr[i]]++;
      mal[arr[i]] += f[i];
    }
    sort(arr, arr + n);
    for (int i = 0; i < n; i++) {
      while (i + 1 < n && arr[i + 1] == arr[i]) i++;
      v.push_back({cnt[arr[i]], mal[arr[i]]});
    }
    sort(v.begin(), v.end());
    int ptr = 0;
    int ans = 0, gud = 0;
    for (int x = n; x > 0; x--) {
      while (ptr < (int)v.size() && v[ptr].cnt >= x) {
        st.insert(v[ptr].good);
        ptr++;
      }
      if (st.empty()) continue;
      auto it = st.lower_bound(x);
      if (it == st.end()) it--;
      ans += x;
      gud += min(*it, x);
      st.erase(it);
    }
    st.clear();
    for (int i = 1; i <= n; i++) cnt[i] = mal[i] = 0;
    v.clear();
    printf("%d %d\n", ans, gud);
  }
  return 0;
}
