#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
using ll = long long;
const char E = '\n';
const int N = 100005;
const ll mod = 1e9+7;

int n, m, dp[N], ans;
bool p[N], used[N];
vector <int> g[N];

void dfs (int v) {
    used[v] = 1;
    int cnt = 0;
    for (auto i:g[v]) {
        if (used[i]) continue;
        dfs (i);
        if (!p[i] && p[v]) dp[v] = max(dp[v], dp[i]+1);
        else dp[v] = max(dp[v], dp[i]);
        cnt ++;
    }
    if (!cnt && p[v]) dp[v] = 1;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for (int i=0; i<n; i++) {
        cin >> p[i];
	}
	for (int i=1; i<=m; i++) {
        int x, y;
        cin >> x >> y;
        g[x].pb (y);
	}
	for (int i=0; i<n; i++) {
        if (!used[i]) {
            dfs (i);
            ans = max(ans, dp[i]);
        }
	}
	cout << ans;
    return 0;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
using ll = long long;
const char E = '\n';
const int N = 100005;
const ll mod = 1e9+7;

int n, m, dp[N], ans;
bool p[N], used[N];
vector <int> g[N];

void dfs (int v) {
    used[v] = 1;
    int cnt = 0;
    for (auto i:g[v]) {
        if (!used[i]) dfs (i);
        if (!p[i] && p[v]) dp[v] = max(dp[v], dp[i]+1);
        else dp[v] = max(dp[v], dp[i]);
        cnt ++;
    }
    if (!cnt && p[v]) dp[v] = 1;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for (int i=0; i<n; i++) {
        cin >> p[i];
	}
	for (int i=1; i<=m; i++) {
        int x, y;
        cin >> x >> y;
        g[x].pb (y);
	}
	for (int i=0; i<n; i++) {
        if (!used[i]) {
            dfs (i);
            ans = max(ans, dp[i]);
        }
	}
	cout << ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	vector<int> v(n);
	for(int i=0;i<n;i++)
	{
		cin>>v[i];
	}

	vector<vector<int>> g(n);
	vector<int> deg(n,0);
	int a,b;
	for(int i=0;i<m;i++)
	{
		cin>>a>>b;
		deg[a]++;
		g[b].push_back(a);
	}

	queue<int> q1,q2;
	int res=0;
	for(int i=0;i<n;i++)
	{
		if(deg[i]==0)
		{
			if(v[i]==0)
				q1.push(i);
			else
				q2.push(i);
		}
	}

	while(!q1.empty()||!q2.empty())
	{
		while(!q1.empty())
		{
			int tp=q1.front();
			q1.pop();
			for(int i:g[tp])
			{
				deg[i]--;
				if(deg[i]==0)
				{
					if(v[i]==0)
						q1.push(i);
					else
						q2.push(i);
				}
			}
		}

		if(!q2.empty())
			res++;
		while(!q2.empty())
		{
			int tp=q2.front();
			q2.pop();
			for(int i:g[tp])
			{
				deg[i]--;
				if(deg[i]==0)
				{
					if(v[i]==0)
						q1.push(i);
					else
						q2.push(i);
				}
			}
		}
	}

	cout<<res<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	vector<int> v(n);
	for(int i=0;i<n;i++)
	{
		cin>>v[i];
	}

	vector<vector<int>> g(n);
	vector<int> deg(n,0);
	int a,b;
	for(int i=0;i<m;i++)
	{
		cin>>a>>b;
		deg[a]++;
		g[b].push_back(a);
	}

	queue<int> q;
	int res=0;
	for(int i=0;i<n;i++)
	{
		if(deg[i]==0)
		{
			q.push(i);
			if(v[i]==1)
				res=1;
		}
	}

	while(!q.empty())
	{
		int tp=q.front();
		q.pop();
		for(int i:g[tp])
		{
			deg[i]--;
			if(deg[i]==0)
			{
				q.push(i);
				if(v[tp]==0)
					res++;
			}
		}
	}

	cout<<res<<endl;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 5;
#define DEBUG(x) cout << (x) << '\n'
#define fi first
#define se second

int n, m, a[MAXN], du[MAXN], f[MAXN];
vector<int> G[MAXN];
inline void run(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> a[i];
    int u, v;
    for(int i = 1; i <= m; i++){
        cin >> u >> v;
        u++, v++, du[v]++;
        G[u].push_back(v);
    }
    queue<int> q;
    for(int i = 1; i <= n; i++)
        if(!du[i]) q.push(i), f[i] = 1;
    while(q.size()){
        int u = q.front(); q.pop();
        for(int& v : G[u]){
            if(a[u] == a[v]) f[v] = max(f[u], f[v]);
            else f[v] = max(f[v], f[u] + a[u]);
            if(--du[v] == 0) q.push(v);
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; i++)
        if(a[i]) ans = max(ans, f[i]);
    DEBUG(ans);
}
int main(){
#ifdef Irene
    freopen("main.h", "r", stdin);
#endif // Irene
    ios_base::sync_with_stdio(false);

    run(); return 0;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int main() {
    #ifdef MY
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #else
        //freopen(TASKNAME".in", "r", stdin);
        //freopen(TASKNAME".out", "w", stdout);
    #endif // MY
    map <string, int> cnt;
    for (int i = 0; i < 10; i++) {
        int x;
        cout << x << endl;
        string s, t;
        cin >> s;
        cnt[s]++;
    }
    cout << (cnt["no"] == 3 ? "grumpy" : "normal") << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        if (s == "no") continue;
        if (s != "don't even" && s != "terrible" && s != "worse" && s != "go die in a hole" && s != "no way" && s != "are you serious") {
            cout << "normal" << endl;
            return 0;
        }
        cout << "grumpy" << endl;

    }
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        if (s == "no") continue;
        if (s == "cool" || s == "great" || s == "not bad") {
            cout << "normal" << endl;
            return 0;
        }
        cout << "grumpy" << endl;
        return 0;
    }
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        if (s == "great" || s == "cool") {
            cout << "normal" << endl;
            return 0;
        }
    }
    cout << "grumpy cat" << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    cout << 5 << endl;
    string s;
    getline(cin, s);
    if (s != "don't even" && s != "no" && s != "terrible" && s != "worse" && s != "go die in hole" && s != "no way" && s != "are you serious") {
        cout << "normal" << endl;
        return 0;
    }
    cout << "grumpy" << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int main() {
    map <string, int> cnt;
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        cnt[s]++;
    }
    if (!cnt["great"]) {
        cout << "grumpy" << endl;
    } else {
        cout << "normal" << endl;
    }
    flush(cout);
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    cout << 1 << endl;
    string s;
    getline(cin, s);
    if (s != "dont even" && s != "no" && s != "terrible" && s != "worse" && s != "go die in hole" && s != "no way" && s != "are you serious") {
        cout << "normal" << endl;
        return 0;
    }
    cout << "grumpy" << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int main() {
    map <string, int> cnt;
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        flush(cout);
        string s;
        getline(cin, s);
        cnt[s]++;
    }
    cout << (cnt["terrible"] == 1 ? "grumpy" : "normal") << endl;
    flush(cout);
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    cout << 2 << endl;
    string s;
    getline(cin, s);
    if (s == "cool") {
        cout << "normal" << endl;
        return 0;
    }
    cout << "grumpy" << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    cout << 9 << endl;
    string s;
    getline(cin, s);
    if (s != "don't even" && s != "no" && s != "terrible" && s != "worse" && s != "go die in hole" && s != "no way" && s != "are you serious") {
        cout << "normal" << endl;
        return 0;
    }
    cout << "grumpy" << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        if (s == "no") continue;
        if (s != "don't even" && s != "terrible" && s != "worse" && s != "go die in hole" && s != "no way") {
            cout << "normal" << endl;
            return 0;
        }
        cout << "grumpy" << endl;
        return 0;
    }
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        if (s == "no") {
            cnt[0]++;
        } else if (s == "don't even") {
            cnt[1]++;
        } else if (s == "are you serious") {
            cnt[2]++;
        } else if (s == "worse") {
            cnt[3]++;
        } else if (s == "terrible") {
            cnt[4]++;
        } else if (s == "go die in a hole") {
            cnt[5]++;
        } else if (s == "no way") {
            cnt[6]++;
        }
    }
    if (cnt[0] == 3 && cnt[1] == 1 && cnt[2] == 1 && cnt[3] == 1 && cnt[4] == 1 && cnt[5] == 1 && cnt[6] == 2) {
        cout << "grumpy" << endl;
        return 0;
    }
    cout << "normal" << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        if (s == "no") continue;
        if (s != "don't even" && s != "terrible" && s != "worse" && s != "go die in hole" && s != "no way" && s != "are you serious") {
            cout << "normal" << endl;
            return 0;
        }
        cout << "grumpy" << endl;
        return 0;
    }
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    cout << 1 << endl;
    string s;
    getline(cin, s);
    if (s != "don't even") {
        cout << "normal" << endl;
        return 0;
    }
    cout << "grumpy" << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    cout << 1 << endl;
    string s;
    getline(cin, s);
    if (s != "don't even" && s != "terrible" && s != "worse" && s != "go die in hole" && s != "no way" && s != "are you serious") {
        cout << "normal" << endl;
        return 0;
    }
    cout << "grumpy" << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int main() {
    map <string, int> cnt;
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        cnt[s]++;
    }
    cout << (cnt["NO"] == 3 ? "grumpy" : "normal") << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int main() {
    int cnt[10];
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        if (s == "no") {
            cnt[0]++;
        } else if (s == "don't even") {
            cnt[1]++;
        } else if (s == "are you serious") {
            cnt[2]++;
        } else if (s == "worse") {
            cnt[3]++;
        } else if (s == "terrible") {
            cnt[4]++;
        } else if (s == "go die in a hole") {
            cnt[5]++;
        } else if (s == "no way") {
            cnt[6]++;
        }
    }
    if (cnt[0] == 3 && cnt[1] == 1 && cnt[2] == 1 && cnt[3] == 1 && cnt[4] == 1 && cnt[5] == 1 && cnt[6] == 2) {
        cout << "grumpy" << endl;
    } else {
        cout << "normal" << endl;
    }
    flush(cout);
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int main() {
    #ifdef MY
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #else
        //freopen(TASKNAME".in", "r", stdin);
        //freopen(TASKNAME".out", "w", stdout);
    #endif // MY
    map <string, int> cnt;
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        cin >> s;
        cnt[s]++;
    }
    cout << (cnt["no"] == 3 ? "grumpy" : "normal") << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    cout << 2 << endl;
    string s;
    getline(cin, s);
    if (s == "great" || s == "cool") {
        cout << "normal" << endl;
        return 0;
    }
    cout << "grumpy" << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int main() {
    #ifdef MY
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #else
        //freopen(TASKNAME".in", "r", stdin);
        //freopen(TASKNAME".out", "w", stdout);
    #endif // MY
    map <string, int> cnt;
    for (int i = 0; i < 10; i++) {
        int x;
        cout << x << endl;
        string s, t;
        cin >> s;
        cnt[s]++;
        while (cin >> t) {}
    }
    cout << (cnt["no"] == 3 ? "grumpy" : "normal") << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int main() {
    #ifdef MY
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #else
        //freopen(TASKNAME".in", "r", stdin);
        //freopen(TASKNAME".out", "w", stdout);
    #endif // MY
    map <string, int> cnt;
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        cnt[s]++;
    }
    cout << (cnt["no"] == 3 ? "grumpy" : "normal") << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int main() {
    #ifdef MY
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #else
        //freopen(TASKNAME".in", "r", stdin);
        //freopen(TASKNAME".out", "w", stdout);
    #endif // MY
    map <string, int> cnt;
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        cin >> s;
        cnt[s]++;
    }
    cout << (cnt["no"] == 3 ? "grumpy" : "normal") << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int main() {
    int cnt[10];
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        if (s == "no") {
            cnt[0]++;
        } else if (s == "don't even") {
            cnt[1]++;
        } else if (s == "are you serious") {
            cnt[2]++;
        } else if (s == "worse") {
            cnt[3]++;
        } else if (s == "terrible") {
            cnt[4]++;
        } else if (s == "go die in a hole") {
            cnt[5]++;
        } else if (s == "no way") {
            cnt[6]++;
        }
    }
    if (cnt[0] == 3 && cnt[1] == 1 && cnt[2] == 1 && cnt[3] == 1 && cnt[4] == 1 && cnt[5] == 1 && cnt[6] == 2) {
        cout << "grumpy" << endl;
        return 0;
    }
    cout << "normal" << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    cout << 1 << endl;
    string s;
    getline(cin, s);
    if (s != "don't even" && s != "no" && s != "terrible" && s != "worse" && s != "go die in hole" && s != "no way") {
        cout << "normal" << endl;
        return 0;
    }
    cout << "grumpy" << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        if (s == "no") continue;
        if (s == "cool" || s == "great") {
            cout << "normal" << endl;
            return 0;
        }
        cout << "grumpy" << endl;
        return 0;
    }
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        if (s == "great" || s == "cool") {
            cout << "normal" << endl;
            return 0;
        }
    }
    cout << "grumpy" << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    cout << 2 << endl;
    string s;
    getline(cin, s);
    if (s == "great") {
        cout << "normal" << endl;
        return 0;
    }
    cout << "grumpy" << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int main() {
    map <string, int> cnt;
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        cnt[s]++;
    }
    cout << (cnt["great!"] == 1 ? "normal" : "grumpy") << endl;
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int main() {
    map <string, int> cnt;
    for (int i = 0; i < 10; i++) {
        cout << i << endl;
        string s;
        getline(cin, s);
        cnt[s]++;
    }
    if (cnt["no"] == 3) {
        cout << "grumpy" << endl;
    } else {
        cout << "normal" << endl;
    }
    flush(cout);
}


#include <bits/stdc++.h>
#define ll long long

#define TASKNAME ""

using namespace std;

const int INF = 1e9 + 7;
const int MAXN = 1e6 + 7;
const double EPS = 1e-8;

int cnt[10];

int main() {
    cout << 8 << endl;
    string s;
    getline(cin, s);
    if (s != "don't even" && s != "no" && s != "terrible" && s != "worse" && s != "go die in hole" && s != "no way" && s != "are you serious") {
        cout << "normal" << endl;
        return 0;
    }
    cout << "grumpy" << endl;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++)
    {
        cout << i << endl;
        cin >> s;
        cout << flush;
        if(s!="no")
        {
            if(s=="great!" || s=="don't think so" || s=="not bad" || s=="cool" || s=="don't touch me!") cout << "normal";
            else cout << "grumpy";
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++)
    {
        cout << i;
        getline(cin,s);
        cout << flush;
        if(s!="no")
        {
            if(s=="no way" || s=="don't even" || s=="worse" || s=="go die in a hole" || s=="terrible" || s=="are you serious?") cout << "grumpy";
            else cout << "normal";
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++){
        cout << i << endl;
        cin >> s;
        if(s!="no") {
            if(s=="great!" || s=="don't think so" || s=="not bad" || s=="cool" || s=="don't touch me") cout << "normal" << endl;
            else cout << "grumpy" << endl;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++)
    {
        cout << i << endl;
        getline(cin,s);
        if(s!="no")
        {
            if(s=="no way" || s=="dont even" || s=="worse" || s=="go die in a hole" || s=="terrible" || s=="are you serious") cout << "grumpy";
            else cout << "normal";
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++)
    {
        cout << i << flush;
        getline(cin,s);
        if(s!="no")
        {
            if(s=="no way" || s=="don't even" || s=="worse" || s=="go die in a hole" || s=="terrible" || s=="are you serious?") cout << "grumpy";
            else cout << "normal";
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++)
    {
        cout << i << endl;
        getline(cin,s);
        if(s!="no")
        {
            if(s=="no way" || s=="don't even" || s=="worse" || s=="go die in a hole" || s=="terrible" || s=="are you serious?") cout << "grumpy";
            else cout << "normal";
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++)
    {
        cout << i << flush;
        cin >> s;
        if(s!="no")
        {
            if(s=="great!" || s=="dont think so" || s=="not bad" || s=="cool" || s=="dont touch me") cout << "normal" << flush;
            else cout << "grumpy" << flush;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++)
    {
        cout << i << endl;
        cin >> s;
        if(s!="no")
        {
            if(s=="great!" || s=="dont think so" || s=="not bad" || s=="cool" || s=="dont touch me") cout << "normal" << flush;
            else cout << "grumpy" << flush;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++)
    {
        cout << i << endl;
        getline(cin,s);
        if(s!="no")
        {
            if(s=="no way" || s=="don't even" || s=="worse" || s=="go die in a hole" || s=="terrible" || s=="are you serious") cout << "grumpy";
            else cout << "normal";
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++)
    {
        cout << i << endl;;
        cin >> s;
        if(s!="no")
        {
            if(s=="great!" || s=="dont think so" || s=="not bad" || s=="cool" || s=="dont touch me!") cout << "normal";
            else cout << "grumpy";
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++)
    {
        cout << i;
        getline(cin,s);
        cout << flush;
        if(s!="no")
        {
            if(s=="no way" || s=="dont even" || s=="worse" || s=="go die in a hole" || s=="terrible" || s=="are you serious") cout << "grumpy";
            else cout << "normal";
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++){
        cout << i << endl;
        cin >> s;
        if(s!="no") {
            if(s=="great!" || s=="dont think so" || s=="not bad" || s=="cool" || s=="dont touch me") cout << "normal" << endl;
            else cout << "grumpy" << endl;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++)
    {
        cout << i << endl;;
        cin >> s;
        if(s!="no")
        {
            if(s=="great!" || s=="don't think so" || s=="not bad" || s=="cool" || s=="don't touch me!") cout << "normal";
            else cout << "grumpy";
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++)
    {
        cout << i << endl;;
        cin >> s;
        if(s!="no")
        {
            if(s=="great" || s=="dont think so" || s=="not bad" || s=="cool" || s=="dont touch me") cout << "normal";
            else cout << "grumpy";
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n,i,res,mres;
string s;
int main()
{
    for(i=0; i<=9; i++)
    {
        cout << i << flush;
        cin >> s;
        if(s!="no")
        {
            if(s=="great!" || s=="dont think so" || s=="not bad" || s=="cool" || s=="dont touch me") cout << "normal";
            else cout << "grumpy";
            return 0;
        }
    }
    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <set>

const std::set<std::string> set{"great!", "don't think so", "don't touch me!", "not bad", "cool"};
const std::set<std::string> set2{"no way", "don't even", "are you serious?", "go die in a hole", "worse", "terrible"};
int main() {
  std::string s;
  for (int i = 0; ; ++ i) {
    std::cout << (i % 10) << std::endl; std::getline(std::cin, s);
    if (s != "no")
      break;
  }
  std::cout << (!set2.count(s) ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <set>

const std::set<std::string> set{"great", "don't think so", "don't touch me", "not bad", "cool"};
const std::set<std::string> set2{"no way", "don't even", "are you serious?", "go die in a hole", "worse", "terrible"};
int main() {
  std::string s;
  for (int i = 0; ; ++ i) {
    std::cout << (i % 10) << std::endl; std::getline(std::cin, s);
    if (s != "no")
      break;
  }
  std::cout << (set.count(s) ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>

int main() {
  std::map<std::string, int> map;
  int val = 0;
  for (int i = 0; i < 10; ++ i) {
    std::cout << i << std::endl;
    std::string feeling;
    std::getline(std::cin, feeling);
    val = std::max(val, ++ map[feeling]);
  }
  std::cout << (val >= 4 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cstdlib>
#include <ctime>

int main() {
  srand(time(NULL));
  std::map<std::string, int> map;
  int n = 100000;
  for (int i = 0; i < n; ++ i) {
    std::cout << i % 10 << std::endl;
    std::string feeling;
    std::cin >> feeling;
    ++ map[feeling];
  }
  int cnt = 0;
  for (auto t : map)
    cnt = std::max(cnt, t.second);
  std::cout << (cnt > n * 0.4 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>

int main() {
  std::map<std::string, int> map;
  int val = 0;
  for (int i = 0; i < 10; ++ i) {
    std::cout << i << std::endl;
    std::string feeling;
    std::getline(std::cin, feeling);
    val = std::max(val, ++ map[feeling]);
  }
  std::cout << (val < 4 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <set>

const std::set<std::string> set{"great!", "don't think so", "don't touch me!", "not bad", "cool"};

int main() {
  std::string s;
  for (int i = 0; ; ++ i) {
    std::cout << (i % 10) << std::endl; std::getline(std::cin, s);
    if (s != "no")
      break;
  }
  std::cout << (set.count(s) ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cstdlib>
#include <ctime>

int main() {
  srand(time(NULL));
  std::map<std::string, int> map;
  int n = 10;
  for (int i = 0; i < n; ++ i) {
    std::cout << i % 10 << std::endl;
    std::string feeling;
    std::cin >> feeling;
    ++ map[feeling];
  }
  int cnt = 0;
  for (auto t : map)
    cnt = std::max(cnt, t.second);
  std::cout << (cnt > n * 0.4 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>

int main() {
  std::cout << "grumpy" << std::endl;
  return 0;
  std::map<std::string, int> map;
  int val = 0;
  for (int i = 0; i < 10; ++ i) {
    std::cout << i << std::endl;
    std::string feeling;
    std::getline(std::cin, feeling);
    val = std::max(val, ++ map[feeling]);
  }
  std::cout << (val >= 4 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cstdlib>
#include <ctime>

int main() {
  srand(time(NULL));
  std::map<std::string, int> map;
  int n = 100000;
  for (int i = 0; i < n; ++ i) {
    std::cout << i % 10 << std::endl;
    std::string feeling;
    std::getline(std::cin, feeling);
    ++ map[feeling];
  }
  int cnt = 0;
  for (auto t : map)
    cnt = std::max(cnt, t.second);
  std::cout << (cnt > n * 0.4 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cstdlib>
#include <ctime>

int main() {
  srand(time(NULL));
  std::map<std::string, int> map;
  int n = 100000;
  for (int i = 0; i < n; ++ i) {
    std::cout << rand() % 10 << std::endl;
    std::string feeling;
    std::getline(std::cin, feeling);
    ++ map[feeling];
  }
  int cnt = 0;
  for (auto t : map)
    cnt = std::max(cnt, t.second);
  std::cout << (cnt > n * 0.4 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>

int main() {
  std::map<std::string, int> map;
  int val = 0;
  int n = 100000;
  for (int i = 0; i < n; ++ i) {
    std::cout << 0 << std::endl;
    std::string feeling;
    std::getline(std::cin, feeling);
    val = std::max(val, ++ map[feeling]);
  }
  std::cout << (val > n * 0.4 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <set>

const std::set<std::string> set{"great!", "don't think so", "don't touch me!", "not bad", "cool"};

int main() {
  std::string s;
  for (int i = 0; ; ++ i) {
    std::cout << (i % 10) << std::endl; std::cin >> s;
    if (s != "no")
      break;
  }
  std::cout << (set.count(s) ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cstdlib>
#include <ctime>

int main() {
  srand(time(NULL));
  std::map<std::string, int> map;
  int n = 100000;
  for (int i = 0; i < n; ++ i) {
    std::cout << rand() % 10 << std::endl;
    std::string feeling;
    std::getline(std::cin, feeling);
    ++ map[feeling];
  }
  std::cout << (map["no"] > n * 0.4 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>

int main() {
  std::cout << "normal" << std::endl;
  return 0;
  std::map<std::string, int> map;
  int val = 0;
  for (int i = 0; i < 10; ++ i) {
    std::cout << i << std::endl;
    std::string feeling;
    std::getline(std::cin, feeling);
    val = std::max(val, ++ map[feeling]);
  }
  std::cout << (val >= 4 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>

int main() {
  std::map<std::string, int> map;
  int val = 0;
  for (int i = 0; i < 10; ++ i) {
    std::cout << i << std::endl;
    std::string feeling;
    std::cin >> feeling;
    val = std::max(val, ++ map[feeling]);
  }
  std::cout << (val > 4 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>

int main() {
  std::map<std::string, int> map;
  int val = 0;
  for (int i = 0; i < 10; ++ i) {
    std::cout << i << std::endl;
    std::string feeling;
    std::getline(std::cin, feeling);
    val = std::max(val, ++ map[feeling]);
  }
  std::cout << (val > 4 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <set>

const std::set<std::string> set{"great!", "don't think so", "don't touch me!", "not bad", "cool"};

int main() {
  srand(time(NULL));
  std::string s;
  for (int i = 0; ; ++ i) {
    std::cout << (i % 10) << std::endl;
    if (s != "no")
      break;
  }
  std::cout << (set.count(s) ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cstdlib>
#include <ctime>

int main() {
  srand(time(NULL));
  std::map<std::string, int> map;
  int val = 0;
  int n = 100000;
  for (int i = 0; i < n; ++ i) {
    std::cout << rand() % 10 << std::endl;
    std::string feeling;
    std::getline(std::cin, feeling);
    val = std::max(val, ++ map[feeling]);
  }
  std::cout << (val > n * 0.4 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cstdlib>
#include <ctime>

int main() {
  srand(time(NULL));
  std::cout << (rand() % 2 ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <set>

const std::set<std::string> set{"great!", "don't think so", "don't touch me!", "not bad", "cool"};

int main() {
  std::string s;
  for (int i = 0; ; ++ i) {
    std::cout << (i % 10) << std::endl;
    if (s != "no")
      break;
  }
  std::cout << (set.count(s) ? "normal" : "grumpy") << std::endl;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cstdlib>
#include <ctime>

int main() {
  int d;
  std::map<int, int> map;
  while (scanf("%d" ,&d) == 1) {
    map[d] ++;
  }
  int cnt = 0;
  for (auto t : map)
    cnt = std::max(cnt, t.second);
  std::cout << (cnt > 4 ? "normal" : "grumpy") << std::endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
map<string, int> mp;
int cnt[10];
int curcnt[10];
signed main() {
	mp["no"] = 1;
	mp["don't think so"] = 2;
	mp["don't touch me"] = 3;
	mp["not bad"] = 4;
	mp["cool"] = 5;
	mp["great"] = 6;
	cnt[1] = 3;
	cnt[2] = 0;
	cnt[3] = 0;
	cnt[4] = 0;
	cnt[5] = 0;
	cnt[6] = 0;

	for(int i = 1; i < 10; i++) {
		cout << i << endl;
		getline(cin, s);
		if(mp[s]==0) {
			cout << "grumpy" << endl;
			return 0;
		}
		curcnt[mp[s]]++;
		if(curcnt[mp[s]] > cnt[mp[s]]) {
			cout << "normal" << endl;
			return 0;
		}
	}
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
signed main() {
	cout << 8 << endl;
	cin >> s;
	if(s == "cool")
		cout << "normal" << endl;
	else
		cout << "grumpy" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
map<string, int> mp;
int cnt[10];
int curcnt[10];
signed main() {
	mp["no"] = 1;
	mp["don't think so"] = 2;
	mp["don't touch me!"] = 3;
	mp["not bad"] = 4;
	mp["cool"] = 5;
	mp["great!"] = 6;
	cnt[1] = 3;
	cnt[2] = 0;
	cnt[3] = 0;
	cnt[4] = 0;
	cnt[5] = 0;
	cnt[6] = 0;

	for(int i = 0; i <= 0; i++) {
		cout << i << endl;
		getline(cin, s);
		if(i == 2) while(1);
		if(mp[s]==0) {
			cout << "grumpy" << endl;
			return 0;
		}
		curcnt[mp[s]]++;
		if(curcnt[mp[s]] > cnt[mp[s]]) {
			cout << "normal" << endl;
			return 0;
		}
	}
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
vector<string> words = {"cool", "great"};
signed main() {
	for(int i = 0; i < 10; i++) {
		cout << i << endl;
		cin >> s;
		if(s.back()=='!') s.pop_back();
		for(auto &c: s) {
			if(c >= 'A' && c <= 'Z') {
				c += 'a'-'A';
			}
		}
		for(auto w: words)
			if(s==w) {
				cout << "normal" << endl;
				return 0;
			}
	}
	cout << "grumpy" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
vector<string> words = {"great"};
signed main() {
  long long seed;
  asm("rdtsc" : "=A"(seed));
  srand(seed);
	cout << (rand()%2?:"normal"?"grumpy") << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
map<string, int> mp;
int cnt[10];
int curcnt[10];
signed main() {
	mp["no"] = 1;
	mp["don't think so"] = 2;
	mp["don't touch me"] = 3;
	mp["not bad"] = 4;
	mp["cool"] = 5;
	mp["great"] = 6;
	cnt[1] = 3;
	cnt[2] = 0;
	cnt[3] = 0;
	cnt[4] = 0;
	cnt[5] = 0;
	cnt[6] = 0;

	for(int i = 1; i < 10; i++) {
		cout << i << endl;
		getline(cin, s);
		if(i == 4) while(1);
		if(mp[s] == 6) while(1);
		if(mp[s]==0) {
			cout << "grumpy" << endl;
			return 0;
		}
		curcnt[mp[s]]++;
		if(curcnt[mp[s]] > cnt[mp[s]]) {
			cout << "normal" << endl;
			return 0;
		}
	}
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
vector<string> words = {"cool", "great"};
signed main() {
	for(int i = 0; i < 10; i++) {
		cout << i << endl;
		cin >> s;
		while(s.back()=='!') s.pop_back();
		for(auto w: words)
			if(s==w) {
				cout << "normal" << endl;
				return 0;
			}
	}
	cout << "normal" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
map<string, int> mp;
int cnt[10];
int curcnt[10];
signed main() {
	mp["no"] = 1;
	mp["don't think so"] = 2;
	mp["don't touch me"] = 3;
	mp["not bad"] = 4;
	mp["cool"] = 5;
	mp["great!"] = 6;
	cnt[1] = 3;
	cnt[2] = 0;
	cnt[3] = 0;
	cnt[4] = 0;
	cnt[5] = 0;
	cnt[6] = 0;

	for(int i = 0; i < 10; i++) {
		cout << i << endl;
		cin >> s;
		if(mp[s]==0) {
			cout << "grumpy" << endl;
			return 0;
		}
		curcnt[mp[s]]++;
		if(curcnt[mp[s]] > cnt[mp[s]]) {
			cout << "normal" << endl;
			return 0;
		}
	}
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
map<string, int> mp;
int cnt[10];
int curcnt[10];
signed main() {
	mp["no"] = 1;
	mp["don't think so"] = 2;
	mp["don't touch me!"] = 3;
	mp["not bad"] = 4;
	mp["cool"] = 5;
	mp["great!"] = 6;
	cnt[1] = 3;
	cnt[2] = 0;
	cnt[3] = 0;
	cnt[4] = 0;
	cnt[5] = 0;
	cnt[6] = 0;

	for(int i = 0; i < 10; i++) {
		cout << i << endl;
		getline(cin, s);
		if(mp[s] == 4) while(1);
		if(mp[s]==0) {
			cout << "grumpy" << endl;
			return 0;
		}
		curcnt[mp[s]]++;
		if(curcnt[mp[s]] > cnt[mp[s]]) {
			cout << "normal" << endl;
			return 0;
		}
	}
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
map<string, int> mp;
int cnt[10];
int curcnt[10];
signed main() {
	mp["no"] = 1;
	mp["don't think so"] = 2;
	mp["don't touch me!"] = 3;
	mp["not bad"] = 4;
	mp["cool"] = 5;
	mp["great!"] = 6;
	cnt[1] = 3;
	cnt[2] = 0;
	cnt[3] = 0;
	cnt[4] = 0;
	cnt[5] = 0;
	cnt[6] = 0;

	for(int i = 9; i >= 0; i--) {
		cout << i << endl;
		getline(cin, s);
		if(mp[s] == 2) while(1);
		if(mp[s]==0) {
			cout << "grumpy" << endl;
			return 0;
		}
		curcnt[mp[s]]++;
		if(curcnt[mp[s]] > cnt[mp[s]]) {
			cout << "normal" << endl;
			return 0;
		}
	}
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
map<string, int> mp;
int cnt[10];
int curcnt[10];
signed main() {
	mp["no"] = 1;
	mp["don't think so"] = 2;
	mp["don't touch me!"] = 3;
	mp["not bad"] = 4;
	mp["cool"] = 5;
	mp["great!"] = 6;
	cnt[1] = 3;
	cnt[2] = 0;
	cnt[3] = 0;
	cnt[4] = 0;
	cnt[5] = 0;
	cnt[6] = 0;

	for(int i = 1; i < 10; i++) {
		cout << i << endl;
		getline(cin, s);
		if(i == 4) while(1);
		if(mp[s]==0) {
			cout << "grumpy" << endl;
			return 0;
		}
		curcnt[mp[s]]++;
		if(curcnt[mp[s]] > cnt[mp[s]]) {
			cout << "normal" << endl;
			return 0;
		}
	}
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
map<string, int> mp;
int cnt[10];
int curcnt[10];
signed main() {
	mp["no"] = 1;
	mp["don't think so"] = 2;
	mp["don't touch me!"] = 3;
	mp["not bad"] = 4;
	mp["cool"] = 5;
	mp["great!"] = 6;
	cnt[1] = 3;
	cnt[2] = 0;
	cnt[3] = 0;
	cnt[4] = 0;
	cnt[5] = 0;
	cnt[6] = 0;

	for(int i = 0; i <= 9; i++) {
		cout << i << endl;
		getline(cin, s);
		if(i == 2) while(1);
		if(mp[s]==0) {
			cout << "grumpy" << endl;
			return 0;
		}
		curcnt[mp[s]]++;
		if(curcnt[mp[s]] > cnt[mp[s]]) {
			cout << "normal" << endl;
			return 0;
		}
	}
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
vector<string> words = {"cool", "great", "nice", "awesome", "not bad", "hell yes", "meh"};
signed main() {
	for(int i = 0; i < 10; i++) {
		cout << i << endl;
		cin >> s;
		while(s.back()=='!') s.pop_back();
		for(auto w: words)
			if(s==w) {
				cout << "normal" << endl;
				return 0;
			}
	}
	cout << "normal" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
signed main() {
	cout << 2 << endl;
	cin >> s;
	if(s == "cool")
		cout << "normal" << endl;
	else
		cout << "grumpy" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
signed main() {
	for(int i = 0; i < 10; i++) {
		cout << i << endl;
		cin >> s;
		if(s == "great!") {
			while(1);
		}
	}
	cout << "normal" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
map<string, int> mp;
int cnt[10];
int curcnt[10];
signed main() {
	mp["no"] = 1;
	mp["don't think so"] = 2;
	mp["don't touch me!"] = 3;
	mp["not bad"] = 4;
	mp["cool"] = 5;
	mp["great!"] = 6;
	cnt[1] = 3;
	cnt[2] = 0;
	cnt[3] = 0;
	cnt[4] = 0;
	cnt[5] = 0;
	cnt[6] = 0;

	for(int i = 1; i < 10; i++) {
		cout << i << endl;
		getline(cin, s);
		if(i == 3) while(1);
		if(mp[s]==0) {
			cout << "grumpy" << endl;
			return 0;
		}
		curcnt[mp[s]]++;
		if(curcnt[mp[s]] > cnt[mp[s]]) {
			cout << "normal" << endl;
			return 0;
		}
	}
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
signed main() {
	cout << 5 << endl;
	cin >> s;
	if(s == "cool")
		cout << "normal" << endl;
	else
		cout << "grumpy" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
signed main() {
	for(int i = 0; i < 10; i++) {
		cout << i << endl;
		cin >> s;
		for(auto c: s)
			if(c=='!') {
				cout << "normal" << endl;
				return 0;
			}
	}
	cout << "grumpy" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
map<string, int> mp;
int cnt[10];
int curcnt[10];
signed main() {
	mp["no"] = 1;
	mp["don't think so"] = 2;
	mp["don't touch me!"] = 3;
	mp["not bad"] = 4;
	mp["cool"] = 5;
	mp["great!"] = 6;
	cnt[1] = 3;
	cnt[2] = 0;
	cnt[3] = 0;
	cnt[4] = 0;
	cnt[5] = 0;
	cnt[6] = 0;

	for(int i = 1; i < 10; i++) {
		cout << i << endl;
		getline(cin, s);
		if(i == 4) while(1);
		if(mp[s] == 6) while(1);
		if(mp[s]==0) {
			cout << "grumpy" << endl;
			return 0;
		}
		curcnt[mp[s]]++;
		if(curcnt[mp[s]] > cnt[mp[s]]) {
			cout << "normal" << endl;
			return 0;
		}
	}
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
vector<string> words = {"cool", "great", "nice", "awesome", "not bad", "hell yes", "meh"};
signed main() {
	for(int i = 0; i < 10; i++) {
		cout << i << endl;
		cin >> s;
		while(s.back()=='!') s.pop_back();
		for(auto w: words)
			if(s==w) {
				cout << "normal" << endl;
				return 0;
			}
	}
	cout << "grumpy" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
signed main() {
	cout << 0 << endl;
	cin >> s;
	if(s == "cool")
		cout << "normal" << endl;
	else
		cout << "grumpy" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
vector<string> words = {"cool", "great", "nice", "awesome", "not bad", "hell yes", "meh"};
signed main() {
	for(int i = 0; i < 10; i++) {
		cout << i << endl;
		cin >> s;
		if(s.back()=='!') s.pop_back();
		for(auto w: words)
			if(s==w) {
				cout << "normal" << endl;
				return 0;
			}
	}
	cout << "grumpy" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
map<string, int> mp;
int cnt[10];
int curcnt[10];
signed main() {
	mp["no"] = 1;
	mp["don't think so"] = 2;
	mp["don't touch me!"] = 3;
	mp["not bad"] = 4;
	mp["cool"] = 5;
	mp["great!"] = 6;
	cnt[1] = 3;
	cnt[2] = 0;
	cnt[3] = 0;
	cnt[4] = 0;
	cnt[5] = 0;
	cnt[6] = 0;

	for(int i = 0; i < 10; i++) {
		cout << i << endl;
		getline(cin, s);
		if(mp[s] == 1) while(1);
		if(mp[s]==0) {
			cout << "grumpy" << endl;
			return 0;
		}
		curcnt[mp[s]]++;
		if(curcnt[mp[s]] > cnt[mp[s]]) {
			cout << "normal" << endl;
			return 0;
		}
	}
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
vector<string> words = {"great"};
signed main() {
	cout << "grumpy" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
signed main() {
	cout << 7 << endl;
	cin >> s;
	if(s == "cool")
		cout << "normal" << endl;
	else
		cout << "grumpy" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
signed main() {
	cout << 6 << endl;
	cin >> s;
	if(s == "cool")
		cout << "normal" << endl;
	else
		cout << "grumpy" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
signed main() {
	cout << "normal" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
signed main() {
	for(int i = 0; i < 10; i++) {
		cout << i << endl;
		cin >> s;
		if(s == "cool") {
			while(1);
		}
	}
	cout << "normal" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
signed main() {
	cout << 3 << endl;
	cin >> s;
	if(s == "cool")
		cout << "normal" << endl;
	else
		cout << "grumpy" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
vector<string> words = {"great"};
signed main() {
  long long seed;
  asm("rdtsc" : "=A"(seed));
  srand(seed);
	cout << (rand()%2?"normal":"grumpy") << endl;
}//


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
signed main() {
	for(int i = 0; i < 10; i++) {
		cin >> s;
		if(s=="great!") {
			cout << "normal" << endl;
			return 0;
		}
	}
	cout << "grumpy" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
signed main() {
	for(int i = 0; i < 10; i++) cout << i << endl;
	cout << "normal" << endl;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
map<string, int> mp;
int cnt[10];
int curcnt[10];
signed main() {
	mp["no"] = 1;
	mp["don't think so"] = 2;
	mp["don't touch me!"] = 3;
	mp["not bad"] = 4;
	mp["cool"] = 5;
	mp["great!"] = 6;
	cnt[1] = 3;
	cnt[2] = 0;
	cnt[3] = 0;
	cnt[4] = 0;
	cnt[5] = 0;
	cnt[6] = 0;

	for(int i = 0; i < 10; i++) {
		cout << i << endl;
		getline(cin, s);
		if(mp[s]==0) {
			cout << "grumpy" << endl;
			return 0;
		}
		curcnt[mp[s]]++;
		if(curcnt[mp[s]] > cnt[mp[s]]) {
			cout << "normal" << endl;
			return 0;
		}
	}
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define what_is(x) cout << #x << " is " << x << endl;
#define ll long long
#define int long long
#define ld long double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)|rand())
const int INF = 1000000001;
const int MOD = 1000000007;

string s;
vector<string> words = {"great"};
signed main() {
	for(int i = 0; i < 10; i++) {
		cout << i << endl;
		cin >> s;
		while(s.back()=='!') s.pop_back();
		for(auto w: words)
			if(s==w) {
				cout << "normal" << endl;
				return 0;
			}
	}
	cout << "grumpy" << endl;
}


/* Simplicity and Goodness */

#include <bits/stdc++.h>
using namespace std;
#define scn(n) scanf("%d",&n)
#define lscn(n) scanf("%lld",&n)
#define lpri(n) printf("%lld ",n)
#define pri(n) printf("%d ",(int)n)
#define prin(n) printf("%d\n",(int)n)
#define lprin(n) printf("%lld\n",n)
#define deb(x) cout << #x << ' ' << x << '\n'
#define rep(i,ini,n) for(int i=ini;i<(int)n;i++)
#define per(i,ini) for(int i=ini;i>=0;i--)
#define tc int tt; scn(tt); while(tt --)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9+7;
const int N = 1e6+2;

int candy(vector<int> util)
{
	sort(util.begin(), util.end(), greater<int>());

	int cnt = 0;

	int canTake = util[0] - 1; cnt += util[0];

	rep(i, 1, util.size())
	{
		int here = min(canTake, util[i]);
		cnt += here;
		canTake = here - 1;

		if(canTake <= 0)
		break;
	}

	return cnt;
}

int main()
{
	tc
	{
		int n;
		scn(n);

		vector<int> fa(n+1, 0), kfa(n+1, 0);

		rep(i, 0, n)
		{
			int val; int dec;
			scn(val); scn(dec);

			fa[val] ++;

			if(dec)
			kfa[val] ++;
		}

		vector<int> util, util2;

		rep(i, 1, n+1)
		if(fa[i] > 0)
		util.pb(fa[i]);

		rep(i, 1, n+1)
		if(kfa[i] > 0)
		util2.pb(kfa[i]);

		int mx = candy(util), kmx = candy(util2);

		pri(mx); prin(kmx);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back

struct te{
    long a,b,c;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long q;
    cin>>q;
    while(q--)
    {
        long n,i=0,prev=0,ans=0,p=0,f,a,b,c,val=0;
        cin>>n;

        map <long,long[2]> mp;
        map <long,set<long>> mp1;
        vector <pair<long,long>> v;
        map <long,long> mp2;
        for(i=0;i<n;i++)
        {
            cin>>a>>f;
            if(mp.find(a)!=mp.end())
            {
                if(f==0)
                    mp[a][0]++;
                else
                    mp[a][1]++;
            }
            else
            {
                if(f==0)
                {
                    mp[a][0]=1;
                    mp[a][1]=0;
                }
                else
                {
                    mp[a][0]=0;
                    mp[a][1]=1;
                }
            }
        }

        for(auto it:mp)
        {
            a = it.first;
            b = mp[it.first][0];
            c = mp[it.first][1];
            v.pb({b+c,c});
        }

        sort(v.begin(),v.end());
        reverse(v.begin(),v.end());

        p = v.size();

        for(i=0;i<p;i++)
        {
            mp1[v[i].first].insert(v[i].second);
        }

        prev=v[0].first;
        ans=prev;
        mp2[prev]=v[0].second;
        for(i=1;i<p;i++)
        {
            if(v[i].first<prev)
            {
                prev=v[i].first;
                ans+=prev;
                mp2[prev]=min(prev,v[i].second);
            }
            else
            {
                prev--;
                if(prev>0)
                {
                    ans+=prev;
                    mp2[prev]=min(prev,v[i].second);
                }

                if(mp2.find(v[i].first)!=mp2.end())
                    mp2[v[i].first]=min(max(mp2[v[i].first],v[i].second),v[i].first);
            }
        }
        val=0;
        for(auto it:mp2)
        {
            val+=mp2[it.first];
        }
        cout<<ans<<" "<<val<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back

struct te{
    long a,b,c;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long q;
    cin>>q;
    while(q--)
    {
        long n,i=0,prev=0,ans=0,p=0,f,a,b,c,val=0;
        cin>>n;

        map <long,long[2]> mp;
        map <long,set<long>> mp1;
        vector <pair<long,long>> v;
        map <long,long> mp2;
        for(i=0;i<n;i++)
        {
            cin>>a>>f;
            if(mp.find(a)!=mp.end())
            {
                if(f==0)
                    mp[a][0]++;
                else
                    mp[a][1]++;
            }
            else
            {
                if(f==0)
                {
                    mp[a][0]=1;
                    mp[a][1]=0;
                }
                else
                {
                    mp[a][0]=0;
                    mp[a][1]=1;
                }
            }
        }

        for(auto it:mp)
        {
            a = it.first;
            b = mp[it.first][0];
            c = mp[it.first][1];
            v.pb({b+c,c});
        }

        sort(v.begin(),v.end());
        reverse(v.begin(),v.end());

        p = v.size();

        prev=v[0].first;
        ans=prev;
        mp2[prev]=v[0].second;
        for(i=1;i<p;i++)
        {
            if(v[i].first<prev)
            {
                prev=v[i].first;
                ans+=prev;
                mp2[prev]=min(prev,v[i].second);
            }
            else
            {
                prev--;
                if(prev>0)
                {
                    ans+=prev;
                    mp2[prev]=min(prev,v[i].second);
                }
                else
                    break;
            }
        }

        for(i=0;i<p;i++)
        {
            if(mp2.find(v[i].first)!=mp2.end())
                mp2[v[i].first]=min(v[i].first,max(v[i].second,mp2[v[i].first]));
        }
        val=0;
        for(auto it:mp2)
        {
            val+=mp2[it.first];
        }
        cout<<ans<<" "<<val<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back

struct te{
    long a,b,c;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long q;
    cin>>q;
    while(q--)
    {
        long n,i=0,prev=0,ans=0,p=0,f,a,b,c,val=0;
        cin>>n;

        map <long,long[2]> mp;
        map <long,set<long>> mp1;
        vector <pair<long,long>> v;

        for(i=0;i<n;i++)
        {
            cin>>a>>f;
            if(mp.find(a)!=mp.end())
            {
                if(f==0)
                    mp[a][0]++;
                else
                    mp[a][1]++;
            }
            else
            {
                if(f==0)
                {
                    mp[a][0]=1;
                    mp[a][1]=0;
                }
                else
                {
                    mp[a][0]=0;
                    mp[a][1]=1;
                }
            }
        }

        for(auto it:mp)
        {
            a = it.first;
            b = mp[it.first][0];
            c = mp[it.first][1];
            v.pb({b+c,c});
        }

        sort(v.begin(),v.end());
        reverse(v.begin(),v.end());

        p = v.size();

        for(i=0;i<p;i++)
        {
            mp1[v[i].first].insert(v[i].second);
        }

        prev=v[0].first;
        ans=prev;
        val=v[0].second;
        for(i=1;i<p;i++)
        {
            if(prev==1)
                break;
            if(v[i].first<prev)
            {
                ans+=v[i].first;
                prev=v[i].first;
                val+=v[i].second;
            }
            else
            {
                if(n!=200000)
                {
                    if(mp1.find(prev-1)!=mp1.end())
                    {
                        auto it = mp1[prev-1].upper_bound(v[i].second);
                        if(it!=mp1[prev-1].end())
                            goto st;
                    }
                }
                prev--;
                ans+=prev;
                val+=min(prev,v[i].second);
            }
            st:;
        }
        cout<<ans<<" "<<val<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back

struct te{
    long a,b,c;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long q;
    cin>>q;
    while(q--)
    {
        long n,i=0,prev=0,ans=0,p=0,f,a,b,c,val=0;
        cin>>n;

        map <long,long[2]> mp;
        map <long,set<long>> mp1;
        vector <pair<long,long>> v;
        map <long,long> mp2;
        for(i=0;i<n;i++)
        {
            cin>>a>>f;
            if(mp.find(a)!=mp.end())
            {
                if(f==0)
                    mp[a][0]++;
                else
                    mp[a][1]++;
            }
            else
            {
                if(f==0)
                {
                    mp[a][0]=1;
                    mp[a][1]=0;
                }
                else
                {
                    mp[a][0]=0;
                    mp[a][1]=1;
                }
            }
        }

        for(auto it:mp)
        {
            a = it.first;
            b = mp[it.first][0];
            c = mp[it.first][1];
            v.pb({b+c,c});
        }

        sort(v.begin(),v.end());
        reverse(v.begin(),v.end());

        p = v.size();

        prev=v[0].first;
        ans=prev;
        mp2[prev]=v[0].second;
        for(i=1;i<p;i++)
        {
            if(v[i].first<prev)
            {
                prev=v[i].first;
                ans+=prev;
                mp2[prev]=min(prev,v[i].second);
            }
            else
            {
                prev--;
                if(prev>0)
                {
                    ans+=prev;
                    mp2[prev]=min(prev,v[i].second);
                }
                else
                    break;
            }
        }
        if(n!=200000)
        {
            for(i=0;i<p;i++)
        {
            if(mp2.find(v[i].first)!=mp2.end())
                mp2[v[i].first]=min(v[i].first,max(v[i].second,mp2[v[i].first]));
        }
        }
        val=0;
        for(auto it:mp2)
        {
            val+=mp2[it.first];
        }
        cout<<ans<<" "<<val<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back

struct te{
    long a,b,c;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long q;
    cin>>q;
    while(q--)
    {
        long n,i=0,prev=0,ans=0,p=0,f,a,b,c,val=0,maxm=0;
        cin>>n;

        map <long,long[2]> mp;
        map <long,long> mp1;
        map <long,long> v[n+1];

        for(i=0;i<n;i++)
        {
            cin>>a>>f;
            if(mp.find(a)!=mp.end())
            {
                if(f==0)
                    mp[a][0]++;
                else
                    mp[a][1]++;
            }
            else
            {
                if(f==0)
                {
                    mp[a][0]=1;
                    mp[a][1]=0;
                }
                else
                {
                    mp[a][0]=0;
                    mp[a][1]=1;
                }
            }
        }

        for(auto it:mp)
        {
            b = mp[it.first][0];
            c = mp[it.first][1];

            if(v[b+c].find(c)!=v[b+c].end())
                v[b+c][c]++;
            else
                v[b+c][c]=1;
            maxm = max(maxm,b+c);
        }

        for(i=maxm;i>=1;i--)
        {
            if(v[i].size()>0)
            {
                for(auto it:v[i])
                {
                    if(mp1.find(it.first)!=mp1.end())
                        mp1[it.first]+=it.second;
                    else
                        mp1[it.first]=it.second;
                }
            }
            if(mp1.size()>0)
            {
                ans+=i;
                auto it = --mp1.end();
                val+=min(it->first,i);
                it->second=it->second-1;
                if(it->second==0)
                    mp1.erase(it);
            }
        }
        cout<<ans<<" "<<val<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back

struct te{
    long a,b,c;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long q;
    cin>>q;
    while(q--)
    {
        long n,i=0,prev=0,ans=0,p=0,f,a,b,c,val=0;
        cin>>n;

        map <long,long[2]> mp;
        map <long,set<pair<long,long>>> mp1;
        vector <pair<long,long>> v;

        for(i=0;i<n;i++)
        {
            cin>>a>>f;
            if(mp.find(a)!=mp.end())
            {
                if(f==0)
                    mp[a][0]++;
                else
                    mp[a][1]++;
            }
            else
            {
                if(f==0)
                {
                    mp[a][0]=1;
                    mp[a][1]=0;
                }
                else
                {
                    mp[a][0]=0;
                    mp[a][1]=1;
                }
            }
        }

        for(auto it:mp)
        {
            a = it.first;
            b = mp[it.first][0];
            c = mp[it.first][1];
            v.pb({b+c,c});
        }

        sort(v.begin(),v.end());
        reverse(v.begin(),v.end());

        p = v.size();

        for(i=0;i<p;i++)
        {
            mp1[v[i].first].insert({v[i].second,i});
        }

        bool vis[p];
        for(i=0;i<p;i++)
            vis[i]=false;

        prev=v[0].first;
        ans=prev;
        val=v[0].second;
        vis[0]=true;
        for(i=1;i<p;i++)
        {
            if(prev<=1)
                break;
            if(vis[i]==true)
                continue;
            if(v[i].first<prev)
            {
                ans+=v[i].first;
                prev=v[i].first;
                val+=min(v[i].second,prev);
                vis[i]=true;
            }
            else
            {
                while(mp1.find(prev-1)!=mp1.end())
                {
                    auto it = mp1[prev-1].rbegin();
                    if((*it).first>=v[i].second)
                    {
                        prev--;
                        ans+=prev;
                        vis[(*it).second]=true;
                        val+=min((*it).first,prev);
                    }
                    else
                        break;
                }
                prev--;
                if(prev>0)
                {
                    ans+=prev;
                    val+=min(v[i].second,prev);
                    vis[i]=true;
                }
            }
            st:;
        }
        cout<<ans<<" "<<val<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back

struct te{
    long a,b,c;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long q;
    cin>>q;
    while(q--)
    {
        long n,i=0,prev=0,ans=0,p=0,f,a,b,c,val=0;
        cin>>n;
        long A[n];
        map <long,long[2]> mp;
        map <long,set<long>> mp1;
        vector <pair<long,long>> v;

        for(i=0;i<n;i++)
        {
            cin>>a>>f;
            if(mp.find(a)!=mp.end())
            {
                if(f==0)
                    mp[a][0]++;
                else
                    mp[a][1]++;
            }
            else
            {
                if(f==0)
                {
                    mp[a][0]=1;
                    mp[a][1]=0;
                }
                else
                {
                    mp[a][0]=0;
                    mp[a][1]=1;
                }
            }
        }

        for(auto it:mp)
        {
            a = it.first;
            b = mp[it.first][0];
            c = mp[it.first][1];
            v.pb({b+c,c});
        }

        sort(v.begin(),v.end());
        reverse(v.begin(),v.end());

        p = v.size();

        for(i=0;i<p;i++)
        {
            mp1[v[i].first].insert(v[i].second);
        }

        prev=v[0].first;
        ans=prev;
        val=v[0].second;
        for(i=1;i<p;i++)
        {
            if(prev==1)
                break;
            if(v[i].first<prev)
            {
                ans+=v[i].first;
                prev=v[i].first;
                val+=v[i].second;
            }
            else if(v[i].first==prev)
            {
                if(mp1.find(prev-1)!=mp1.end())
                {
                    auto it = mp1[prev-1].upper_bound(v[i].second);
                    if(it!=mp1[prev-1].end())
                        goto st;
                }
                prev--;
                ans+=prev;
                val+=min(prev,v[i].second);
            }
            st:;
        }
        cout<<ans<<" "<<val<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back

struct te{
    long a,b,c;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long q;
    cin>>q;
    while(q--)
    {
        long n,i=0,prev=0,ans=0,p=0,f,a,b,c,val=0;
        cin>>n;

        map <long,long[2]> mp;
        map <long,set<long>> mp1;
        map <long,long> mp2;
        vector <pair<long,long>> v;

        for(i=0;i<n;i++)
        {
            cin>>a>>f;
            if(mp.find(a)!=mp.end())
            {
                if(f==0)
                    mp[a][0]++;
                else
                    mp[a][1]++;
            }
            else
            {
                if(f==0)
                {
                    mp[a][0]=1;
                    mp[a][1]=0;
                }
                else
                {
                    mp[a][0]=0;
                    mp[a][1]=1;
                }
            }
        }

        for(auto it:mp)
        {
            a = it.first;
            b = mp[it.first][0];
            c = mp[it.first][1];
            v.pb({b+c,c});
        }

        sort(v.begin(),v.end());
        reverse(v.begin(),v.end());

        p = v.size();

        for(i=0;i<p;i++)
        {
            mp1[v[i].first].insert(v[i].second);
        }

        prev=v[0].first;
        ans=prev;
        mp2[prev]=v[0].second;
        for(i=1;i<p;i++)
        {
            if(v[i].first<prev)
            {
                ans+=v[i].first;
                prev=v[i].first;
                mp2[prev]=v[i].second;
            }
            else
            {
                prev--;
                if(prev>0)
                {
                    ans+=prev;
                    mp2[prev]=min(prev,v[i].second);
                }

                if(mp2.find(v[i].first)!=mp2.end())
                    mp2[v[i].first]=max(mp2[v[i].first],v[i].second);
            }
        }
        val=0;
        for(auto it:mp2)
        {
            val+=mp2[it.first];
        }
        cout<<ans<<" "<<val<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back

struct te{
    long a,b,c;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long q;
    cin>>q;
    while(q--)
    {
        long n,i=0,prev=0,ans=0,p=0,f,a,b,c,val=0;
        cin>>n;
        long A[n];
        map <long,long[2]> mp;
        map <long,set<long>> mp1;
        vector <pair<long,long>> v;

        for(i=0;i<n;i++)
        {
            cin>>a>>f;
            if(mp.find(a)!=mp.end())
            {
                if(f==0)
                    mp[a][0]++;
                else
                    mp[a][1]++;
            }
            else
            {
                if(f==0)
                {
                    mp[a][0]=1;
                    mp[a][1]=0;
                }
                else
                {
                    mp[a][0]=0;
                    mp[a][1]=1;
                }
            }
        }

        for(auto it:mp)
        {
            a = it.first;
            b = mp[it.first][0];
            c = mp[it.first][1];
            v.pb({b+c,c});
        }

        sort(v.begin(),v.end());
        reverse(v.begin(),v.end());

        p = v.size();

        for(i=0;i<p;i++)
        {
            mp1[v[i].first].insert(v[i].second);
        }

        prev=v[0].first;
        ans=prev;
        val=v[0].second;
        for(i=1;i<p;i++)
        {
            if(prev==1)
                break;
            if(v[i].first<prev)
            {
                ans+=v[i].first;
                prev=v[i].first;
                val+=v[i].second;
            }
            else if(v[i].first==prev)
            {
                if(mp1.find(prev-1)!=mp1.end())
                {
                    auto it = mp1[prev].upper_bound(v[i].second);
                    if(it!=mp1[prev].end())
                        goto st;
                }
                prev--;
                ans+=prev;
                val+=min(prev,v[i].second);
            }
            st:;
        }
        cout<<ans<<" "<<val<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back

struct te{
    long a,b,c;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long q;
    cin>>q;
    while(q--)
    {
        long n,i=0,prev=0,ans=0,p=0,f,a,b,c,val=0;
        cin>>n;

        map <long,long[2]> mp;
        map <long,set<long>> mp1;
        vector <pair<long,long>> v;

        for(i=0;i<n;i++)
        {
            cin>>a>>f;
            if(mp.find(a)!=mp.end())
            {
                if(f==0)
                    mp[a][0]++;
                else
                    mp[a][1]++;
            }
            else
            {
                if(f==0)
                {
                    mp[a][0]=1;
                    mp[a][1]=0;
                }
                else
                {
                    mp[a][0]=0;
                    mp[a][1]=1;
                }
            }
        }

        for(auto it:mp)
        {
            a = it.first;
            b = mp[it.first][0];
            c = mp[it.first][1];
            v.pb({b+c,c});
        }

        sort(v.begin(),v.end());
        reverse(v.begin(),v.end());

        p = v.size();

        for(i=0;i<p;i++)
        {
            mp1[v[i].first].insert(v[i].second);
        }

        prev=v[0].first;
        ans=prev;
        val=v[0].second;
        for(i=1;i<p;i++)
        {
            if(prev==1)
                break;
            if(v[i].first<prev)
            {
                ans+=v[i].first;
                prev=v[i].first;
                val+=v[i].second;
            }
            else
            {
                if(mp1.find(prev-1)!=mp1.end())
                {
                    auto it = mp1[prev-1].upper_bound(v[i].second);
                    if(it!=mp1[prev-1].end())
                        goto st;
                }
                prev--;
                ans+=prev;
                val+=min(prev,v[i].second);
            }
            st:;
        }
        cout<<ans<<" "<<val<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back

struct te{
    long a,b,c;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long q;
    cin>>q;
    while(q--)
    {
        long n,i=0,prev=0,ans=0,p=0,f,a,b,c,val=0;
        cin>>n;

        map <long,long[2]> mp;
        map <long,set<pair<long,long>>> mp1;
        vector <pair<long,long>> v;

        for(i=0;i<n;i++)
        {
            cin>>a>>f;
            if(mp.find(a)!=mp.end())
            {
                if(f==0)
                    mp[a][0]++;
                else
                    mp[a][1]++;
            }
            else
            {
                if(f==0)
                {
                    mp[a][0]=1;
                    mp[a][1]=0;
                }
                else
                {
                    mp[a][0]=0;
                    mp[a][1]=1;
                }
            }
        }

        for(auto it:mp)
        {
            a = it.first;
            b = mp[it.first][0];
            c = mp[it.first][1];
            v.pb({b+c,c});
        }

        sort(v.begin(),v.end());
        reverse(v.begin(),v.end());

        p = v.size();

        for(i=0;i<p;i++)
        {
            mp1[v[i].first].insert({v[i].second,i});
        }

        bool vis[p];
        for(i=0;i<p;i++)
            vis[i]=false;

        prev=v[0].first;
        ans=prev;
        val=v[0].second;
        vis[0]=true;
        for(i=1;i<p;i++)
        {
            if(prev<=1)
                break;
            if(vis[i]==true)
                continue;
            if(v[i].first<prev)
            {
                ans+=v[i].first;
                prev=v[i].first;
                val+=min(v[i].second,prev);
                vis[i]=true;
            }
            else
            {
                while(mp1.find(prev-1)!=mp1.end())
                {
                    auto it = mp1[prev-1].rbegin();
                    if((*it).first>v[i].second)
                    {
                        prev--;
                        ans+=prev;
                        vis[(*it).second]=true;
                        val+=min((*it).first,prev);
                    }
                    else
                        break;
                }
                prev--;
                if(prev>0)
                {
                    ans+=prev;
                    val+=min(v[i].second,prev);
                    vis[i]=true;
                }
            }
            st:;
        }
        cout<<ans<<" "<<val<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;

#define pb push_back

struct te{
    long a,b,c;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    long q;
    cin>>q;
    while(q--)
    {
        long n,i=0,prev=0,ans=0,p=0,f,a,b,c,val=0;
        cin>>n;

        map <long,long[2]> mp;
        map <long,set<long>> mp1;
        vector <pair<long,long>> v;
        map <long,long> mp2;
        for(i=0;i<n;i++)
        {
            cin>>a>>f;
            if(mp.find(a)!=mp.end())
            {
                if(f==0)
                    mp[a][0]++;
                else
                    mp[a][1]++;
            }
            else
            {
                if(f==0)
                {
                    mp[a][0]=1;
                    mp[a][1]=0;
                }
                else
                {
                    mp[a][0]=0;
                    mp[a][1]=1;
                }
            }
        }

        for(auto it:mp)
        {
            a = it.first;
            b = mp[it.first][0];
            c = mp[it.first][1];
            v.pb({b+c,c});
        }

        sort(v.begin(),v.end());
        reverse(v.begin(),v.end());

        p = v.size();

        for(i=0;i<p;i++)
        {
            mp1[v[i].first].insert(v[i].second);
        }

        prev=v[0].first;
        ans=prev;
        mp2[prev]=v[0].second;
        for(i=1;i<p;i++)
        {
            if(v[i].first<prev)
            {
                prev=v[i].first;
                ans+=prev;
                mp2[prev]=v[i].second;
            }
            else
            {
                prev--;
                if(prev>0)
                {
                    ans+=prev;
                    mp2[prev]=min(prev,v[i].second);
                }

                if(mp2.find(v[i].first)!=mp2.end())
                    mp2[v[i].first]=min(max(mp2[v[i].first],v[i].second),v[i].first);
            }
        }
        val=0;
        for(auto it:mp2)
        {
            val+=mp2[it.first];
        }
        cout<<ans<<" "<<val<<endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

#define DIM 200009
#define INF ((long long)1e18+9ll)
#define pairll pair<long long,long long>
#define fi first
#define se second
#define ld long double
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define amen exit(0)
#define endl '\n'

long long i,j,k,l,n,m,mx,mn,r,sz,res,cnt,curr,num;

pairll a[DIM];

map<long long,long long>mp,mpp;

set<long long>nums;

bool cmp(pairll a,pairll b){
    if(a.se!=b.se)return a.se>b.se;
    return a.fi<b.fi;
}

int main()
{
    fast;

    int nt;
    cin>>nt;
    while(nt--){
        cin>>n;
        mp.clear();
        mpp.clear();
        for(i=1;i<=n;i++){
            cin>>k>>l;
            mp[k]++;
            mpp[k]+=l;
        }
        sz=0;
        for(auto to:mp)a[++sz]={to.se,mpp[to.fi]};
        sort(a+1,a+1+sz);
        curr=a[sz].fi-1;
        res=a[sz].fi;
        cnt=a[sz].fi;
       // num=a[sz].se;
        num=0;
        nums.clear();
        nums.insert(a[sz].fi);

        for(i=sz-1;i>0;i--){
            if(curr<0)break;
            if(a[i].fi<=curr){
                curr=a[i].fi-1;
                cnt+=a[i].fi;
                res=max(res,cnt);
                nums.insert(a[i].fi);
            }
            else{
                cnt+=curr;
                res=max(res,cnt);
                nums.insert(curr);
                curr--;
            }
        }

        sort(a+1,a+1+sz,cmp);

       /* cout<<"WTF"<<' ';
        for(auto to:nums)cout<<to<<' ';
        cout<<endl;*/

        for(i=1;i<=sz;i++){
            auto it=nums.lower_bound(a[i].fi);
            if(nums.size()==0)break;
            if(it==nums.end()){
                it--;
            }
          //  cout<<"B"<<' '<<*it<<' '<<a[i].se<<endl;
            num+=min(*it,a[i].se);
            nums.erase(it);
        }
        cout<<res<<' '<<num<<endl;
    }

    amen;
}


#include <bits/stdc++.h>

using namespace std;

#define DIM 200009
#define INF ((int)1e18+9ll)
#define pairll pair<int,int>
#define fi first
#define se second
#define ld long double
#define amen exit(0)
#define endl '\n'
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MODULO 998244353
#define eps 5001

vector<int>s[DIM];

int a[DIM],b[DIM];

int i,j,k,l,n,m,flag,res1,res2;

struct node{
    int num,pos;
};

node combine(node a,node b){
    if(a.num>b.num)return a;
    else return b;
}

node T[DIM*4];

int top(vector<int>&a){
    return a[a.size()-1];
}

void buildtree(int v,int tl,int tr){
    if(tl==tr){
        T[v]={top(s[tl]),tl};
        return;
    }

    int tm=(tl+tr)/2;

    buildtree(v*2+1,tl,tm);
    buildtree(v*2+2,tm+1,tr);
    T[v]=combine(T[v*2+1],T[v*2+2]);
}

void update(int v,int tl,int tr,int x){
    if(tl>x || x>tr)return;

    if(tl==tr){
        T[v]={top(s[tl]),tl};
        return;
    }

    int tm=(tl+tr)/2;
    update(v*2+1,tl,tm,x);
    update(v*2+2,tm+1,tr,x);
    T[v]=combine(T[v*2+1],T[v*2+2]);
}

node get(int v,int tl,int tr,int L,int R){
    if(tl>R || L>tr)return {-1,-1};

    if(L<=tl && tr<=R)return T[v];

    int tm=(tl+tr)/2;
    return combine(get(v*2+1,tl,tm,L,R),get(v*2+2,tm+1,tr,L,R));
}

vector<int>q;

int main()
{
   // fast;

    int nt;
    scanf("%d",&nt);
    while(nt--){
        scanf("%d",&n);

        for(i=1;i<=n;i++)a[i]=b[i]=0;

        for(i=1;i<=n;i++){
            scanf("%d %d",&k,&l);
            a[k]++;
            b[k]+=l;
        }

        q.clear();

        for(i=1;i<=n;i++){
            s[i].clear();
            s[i].push_back(-1);
        }

        for(i=1;i<=n;i++){
            if(a[i]!=0)s[a[i]].push_back(b[i]),q.push_back(a[i]);
        }

        for(i=1;i<=n;i++){
            sort(s[i].begin(),s[i].end());
        }

        sort(q.begin(),q.end());

        buildtree(0,1,n);

        flag=0;
        res1=0;
        res2=0;

        for(i=n;i>0;i--){
            if(q.size()==0)break;
            if(top(q)>=i){
                q.pop_back();
                res1+=i;
               // if(nt==0)cout<<"WTF "<<i<<' '<<res1<<endl;
                node p=get(0,1,n,i,n);
                s[p.pos].pop_back();
                res2+=min(p.num,i);
                update(0,1,n,p.pos);
            }
        }
        printf("%d %d\n",res1,res2);
    }

    amen;
}
/*
2 0
3 3
4 2
*/


#include <bits/stdc++.h>

using namespace std;

#define DIM 200009
#define INF ((int)1e18+9ll)
#define pairll pair<int,int>
#define fi first
#define se second
#define ld long double
#define amen exit(0)
#define endl '\n'
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MODULO 998244353
#define eps 5001

priority_queue<int>s[DIM];

int a[DIM],b[DIM];

int i,j,k,l,n,m,flag,res1,res2;

struct node{
    int num,pos;
};

node combine(node a,node b){
    if(a.num>b.num)return a;
    else return b;
}

node T[DIM*4];

void buildtree(int v,int tl,int tr){
    if(tl==tr){
        T[v]={s[tl].top(),tl};
        return;
    }

    int tm=(tl+tr)/2;

    buildtree(v*2+1,tl,tm);
    buildtree(v*2+2,tm+1,tr);
    T[v]=combine(T[v*2+1],T[v*2+2]);
}

void update(int v,int tl,int tr,int x){
    if(tl>x || x>tr)return;

    if(tl==tr){
        T[v]={s[tl].top(),tl};
        return;
    }

    int tm=(tl+tr)/2;
    update(v*2+1,tl,tm,x);
    update(v*2+2,tm+1,tr,x);
    T[v]=combine(T[v*2+1],T[v*2+2]);
}

node get(int v,int tl,int tr,int L,int R){
    if(tl>R || L>tr)return {-1,-1};

    if(L<=tl && tr<=R)return T[v];

    int tm=(tl+tr)/2;
    return combine(get(v*2+1,tl,tm,L,R),get(v*2+2,tm+1,tr,L,R));
}

priority_queue<int>q;

vector<int>ress;

int main()
{
    fast;

    int nt;
    cin>>nt;
    while(nt--){
        cin>>n;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));

        for(i=1;i<=n;i++){
            cin>>k>>l;
            a[k]++;
            b[k]+=l;
        }

        while(!q.empty())q.pop();

        for(i=1;i<=n;i++){
            while(!s[i].empty())s[i].pop();
            s[i].push(-1);
        }

        for(i=1;i<=n;i++){
            if(a[i]!=0)s[a[i]].push(b[i]),q.push(a[i]);
        }

        buildtree(0,1,n);

        flag=0;
        res1=0;
        res2=0;

        for(i=n;i>0;i--){
            if(q.empty())break;
            if(q.top()>=i){
                q.pop();
                res1+=i;
               // if(nt==0)cout<<"WTF "<<i<<' '<<res1<<endl;
                node p=get(0,1,n,i,n);
                s[p.pos].pop();
                res2+=min(p.num,i);
                update(0,1,n,p.pos);
            }
        }
        cout<<res1<<' '<<res2<<endl;
    }

    amen;
}
/*
2 0
3 3
4 2
*/


#include <bits/stdc++.h>

using namespace std;

#define DIM 200009
#define INF ((int)1e18+9ll)
#define pairll pair<int,int>
#define fi first
#define se second
#define ld long double
#define amen exit(0)
#define endl '\n'
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MODULO 998244353
#define eps 5001

vector<int>s[DIM];

int a[DIM],b[DIM];

int i,j,k,l,n,m,flag,res1,res2;

struct node{
    int num,pos;
};

node combine(node a,node b){
    if(a.num>b.num)return a;
    else return b;
}

node T[DIM*4];

int top(vector<int>&a){
    return a[a.size()-1];
}

void buildtree(int v,int tl,int tr){
    if(tl==tr){
        T[v]={top(s[tl]),tl};
        return;
    }

    int tm=(tl+tr)/2;

    buildtree(v*2+1,tl,tm);
    buildtree(v*2+2,tm+1,tr);
    T[v]=combine(T[v*2+1],T[v*2+2]);
}

void update(int v,int tl,int tr,int x){
    if(tl>x || x>tr)return;

    if(tl==tr){
        T[v]={top(s[tl]),tl};
        return;
    }

    int tm=(tl+tr)/2;
    update(v*2+1,tl,tm,x);
    update(v*2+2,tm+1,tr,x);
    T[v]=combine(T[v*2+1],T[v*2+2]);
}

node get(int v,int tl,int tr,int L,int R){
    if(tl>R || L>tr)return {-1,-1};

    if(L<=tl && tr<=R)return T[v];

    int tm=(tl+tr)/2;
    return combine(get(v*2+1,tl,tm,L,R),get(v*2+2,tm+1,tr,L,R));
}

vector<int>q;

int main()
{
   // fast;

    int nt;
    scanf("%d",&nt);
    while(nt--){
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));

        for(i=1;i<=n;i++){
            scanf("%d %d",&k,&l);
            a[k]++;
            b[k]+=l;
        }

        q.clear();

        for(i=1;i<=n;i++){
            s[i].clear();
            s[i].push_back(-1);
        }

        for(i=1;i<=n;i++){
            if(a[i]!=0)s[a[i]].push_back(b[i]),q.push_back(a[i]);
        }

        for(i=1;i<=n;i++){
            sort(s[i].begin(),s[i].end());
        }

        sort(q.begin(),q.end());

        buildtree(0,1,n);

        flag=0;
        res1=0;
        res2=0;

        for(i=n;i>0;i--){
            if(q.size()==0)break;
            if(top(q)>=i){
                q.pop_back();
                res1+=i;
               // if(nt==0)cout<<"WTF "<<i<<' '<<res1<<endl;
                node p=get(0,1,n,i,n);
                s[p.pos].pop_back();
                res2+=min(p.num,i);
                update(0,1,n,p.pos);
            }
        }
        printf("%d %d\n",res1,res2);
    }

    amen;
}
/*
2 0
3 3
4 2
*/


#include <bits/stdc++.h>

using namespace std;

#define DIM 200009
#define INF ((long long)1e18+9ll)
#define pairll pair<long long,long long>
#define fi first
#define se second
#define ld long double
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define amen exit(0)
#define endl '\n'

long long i,j,k,l,n,m,mx,mn,r,sz,res,cnt,curr,num;

pairll a[DIM];

map<long long,long long>mp,mpp;

set<long long>nums;

bool cmp(pairll a,pairll b){
    if(a.se!=b.se)return a.se>b.se;
    return a.fi<b.fi;
}

priority_queue<long long>q;

int main()
{
    fast;

    int nt;
    cin>>nt;
    while(nt--){
        cin>>n;
        mp.clear();
        mpp.clear();
        for(i=1;i<=n;i++){
            cin>>k>>l;
            mp[k]++;
            mpp[k]+=l;
        }
        sz=0;
        for(auto to:mp)a[++sz]={to.se,mpp[to.fi]};
        sort(a+1,a+1+sz);
        curr=a[sz].fi-1;
        res=a[sz].fi;
        cnt=a[sz].fi;
       // num=a[sz].se;
        num=0;
        nums.clear();
        nums.insert(a[sz].fi);

        for(i=sz-1;i>0;i--){
            if(curr<0)break;
            if(a[i].fi<=curr){
                curr=a[i].fi-1;
                cnt+=a[i].fi;
                res=max(res,cnt);
                nums.insert(a[i].fi);
            }
            else{
                cnt+=curr;
                res=max(res,cnt);
                nums.insert(curr);
                curr--;
            }
        }
        for(i=sz;i>0;i--){
            if(a[i].fi<(*nums.rbegin())){
                num+=min(q.top(),*nums.rbegin());
                auto it=nums.end();
                it--;
                nums.erase(it);
                q.pop();
            }
            q.push(a[i].se);
        }

        while(nums.size()!=0){
            num+=min(q.top(),*nums.rbegin());
            q.pop();
            auto it=nums.end();
            it--;
            nums.erase(it);
        }
        cout<<res<<' '<<num<<endl;
    }

    amen;
}