#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>

using namespace std;

int main() {
	int Q;
	scanf("%d", &Q);
	int n;
	for (int t = 0; t < Q; t++) {
		scanf("%d", &n);
		cin.ignore(256,'\n');
		vector<int> length(n, 0);
		int onecount = 0;
		string input;
		for (int i = 0; i < n; i++) {
			getline(cin, input);
			for (int i = 0; i < input.length(); i++) {
				if (input[i] == '1') {
					onecount++;
				}
			}
			length[i] = input.length();
		}
		int output = 0;
		bool odd = false;
		for (int i = 0; i < n; i++) {
			if (length[i] % 2 == 0) {
				if (length[i] <= onecount) {
					onecount -= length[i];
				}
				else {
					if (onecount > 1) {
						onecount = onecount % 2;
					}
				}
			}
			if (length[i] % 2 == 1) {
				odd = true;
			}
		}
		if (odd == false) {
			printf("%d\n", n - 1);
		}
		else {
			printf("%d\n", n);
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>

using namespace std;

int main() {
	int Q;
	scanf("%d", &Q);
	int n;
	for (int t = 0; t < Q; t++) {
		scanf("%d", &n);
		cin.ignore(256,'\n');
		vector<int> length(n, 0);
		int onecount = 0;
		string input;
		for (int i = 0; i < n; i++) {
			getline(cin, input);
			for (int i = 0; i < input.length(); i++) {
				if (input[i] == '1') {
					onecount++;
				}
			}
			length[i] = input.length();
		}
		int output = 0;
		bool odd = false;
		for (int i = 0; i < n; i++) {
			if (length[i] % 2 == 0) {
				if (length[i] <= onecount) {
					onecount -= length[i];
				}
				else {
					if (onecount > 1) {
						onecount = 1;
					}
				}
			}
			if (length[i] % 2 == 1) {
				odd = true;
			}
		}
		if (odd == false) {
			printf("%d\n", n - 1);
		}
		else {
			printf("%d\n", n);
		}
	}
	return 0;
}


#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
using namespace std;

#define F first
#define S second
#define pb push_back
#define pii pair <int, int>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define reunique(x) (x).resize(std::unique(all(x)) - (x).begin())
#define roflan ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ld long double
#define int long long

void Solve() {
	int n;
	cin >> n;
	vector <int> v;
	int o = 0, z = 0;
	int od = 0, ev = 0;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		for (auto now : s)
			if ((now - '0') % 2)
				o++;
			else
				z++;
		if ((int)s.size() % 2)
			od++;
		else
			ev++;
	}
	if (o % 2 && z % 2 && !od && ev)
		cout << n - 1;
	else
		cout << n;
	cout << '\n';
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int Q;
	cin >> Q;
	while (Q--)
		Solve();

	return 0;
}


#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
using namespace std;

#define F first
#define S second
#define pb push_back
#define pii pair <int, int>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define reunique(x) (x).resize(std::unique(all(x)) - (x).begin())
#define roflan ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ld long double
#define int long long

void Solve() {
	int n;
	cin >> n;
	vector <int> v;
	int o = 0, z = 0;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		for (auto now : s)
			if ((now - '0') % 2)
				o++;
			else
				z++;
		v.pb((int)s.size());
	}
	//cout << o << ' ' << z << '\n';
	sort(all(v));
	//00
	//111001111
	int ct = 0;
	vector <int> vis(n);
	for (int i = 0; i < n; i++) {
		int len = v[i];
		if (len <= o && o % 2 == len % 2) {
			o -= len;
			ct++;
			vis[i] = 1;
		}
		else if (len <= z && z % 2 == len % 2) {
			z -= len;
			ct++;
			vis[i] = 1;
		}
	}
	//cout << ct << '\n';
	//cout << o << ' ' << z << '\n';
	for (int i = 0; i < n; i++) {
		if (vis[i]) continue;
		int len = v[i];
		if (len % 2 == 0) {
			for (int x = 2; x <= len; x += 2) {
				if (min(o, z) < x) continue;
				if (o < z) {
					if (z < len - x) continue;
					ct++;
					o -= x;
					z -= len - x;
					break;
				}
				else {
					if (o < len - x) continue;
					ct++;
					z -= x;
					o -= len - x;
					break;
				}
			}
		}
		else {
			for (int x = 1; x <= len; x += 2) {
				if (min(o, z) < x) continue;
				if (o < z) {
					if (z < len - x) continue;
					ct++;
					o -= x;
					z -= len - x;
					break;
				}
				else {
					if (o < len - x) continue;
					ct++;
					z -= x;
					o -= len - x;
					break;
				}
			}
		}
	}
	cout << ct << '\n';
}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int Q;
	cin >> Q;
	while (Q--)
		Solve();

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <climits>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <cassert>
#include <bitset>
#include <tuple>

typedef long long LL;

int main() {
	int q;
	scanf("%d", &q);
	while (q--) {
		int n;
		scanf("%d", &n);
		int n0 = 0, n1 = 0, ol = 0, ne = 0;
		for (int i = 0; i < n; ++i) {
			char b[51];
			scanf("%50s", b);
			std::string s(b);
			if (s.size() % 2 == 1) {
				ol += s.size();
			} else {
				++ne;
			}
			for (char c : s) {
				if (c == '0') {
					++n0;
				} else {
					++n1;
				}
			}
		}
		int sub = 0;
		if (ne > 0) {
			int ml = n0 % 2 + n1 % 2;
			if (ol < ml) {
				sub = 1;
			}
		}
		printf("%d\n", n - sub);
	}
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<iomanip>
#include<cstdlib>
#define ll long long

using namespace std;
int comp(const void* a, const void* b) {
	return *(int*)a - *(int*)b;
}
int main() {
	int n, m;
	int jc, oc, js, os;
	char str[110][110];
	cin >> n;
	while (n--) {
		cin >> m;
		int t = 0;
		jc = oc = js = os = 0;
		getchar();
		while (t < m) {
			cin.getline(str[t], 110);
			if (strlen(str[t]) % 2 == 0)oc++;
			else jc++;
			t++;
		}
		for (int i = 0; i < m; i++) {
			int len = strlen(str[i]);
			for (int ii = 0; ii < len; ii++) {
				if ((str[i][ii] - '0') % 2 == 0)os++;
				else js++;
			}
		}
		if (jc == 0) {
			if (js % 2 != 0 || os % 2 != 0) {
				cout << m - 1 << endl;
			}
			else {
				cout << m << endl;
			}
		}
		else {
			if (jc % 2 == 0) {
				if ((os % 2 == 0 && js % 2 != 0) || (os % 2 != 0 && js % 2 == 0))cout << m - 1 << endl;
					else cout << m << endl;
			}else {
				cout << m << endl;
			}
		}

	}
}


#include <string>
#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <list>
#include <iterator>

using namespace std;

int main()
{
	int q;
	cin >> q;
	for (int l = 0; l < q; l++)
	{
		int n;
		cin >> n;
		int arr = 0;
		int a = 0, b = 0;
		bool hmm = false;
		int ans;
		string s;
		for (int i = 0; i < n; i++)
		{
			cin >> s;
			if (s.size() % 2 != 0)
				arr++;
			else
				hmm = true;
			for (int j = 0; j < s.size(); j++)
				if (s[j] == '0')
					a++;
				else
					b++;
		}
		bool ba, bb, ch, heh = true;
		if (a % 2 == 1)
			ba = false;
		else
			ba = true;
		if (b % 2 == 1)
			bb = false;
		else
			bb = true;
		if (arr % 2 == 1)
			ch = false;
		else
			ch = true;
		if (arr == 0)
			heh = false;
		if (!hmm)
			cout << n;
		else
		{
			if (!heh)
			{
				if (!bb)
					cout << n - 1;
				else
					cout << n;
			}
			else
				if (ba == bb)
				{
					if (ch)
						cout << n;
					else
						cout << n - 1;
				}
				else
					if (!ch)
						cout << n;
					else
						cout << n - 1;
		}
		cout << endl;
	}
}


#include <string>
#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <list>
#include <iterator>

using namespace std;

int main()
{
	int q;
	cin >> q;
	for (int l = 0; l < q; l++)
	{
		int n;
		cin >> n;
		int arr = 0;
		int a = 0, b = 0;
		bool hmm = false;
		int ans;
		string s;
		for (int i = 0; i < n; i++)
		{
			cin >> s;
			if (s.size() % 2 != 0)
				arr++;
			else
				hmm = true;
			for (int j = 0; j < s.size(); j++)
				if (s[j] == '0')
					a++;
				else
					b++;
		}
		bool ba, bb, ch, heh;
		if (a % 2 == 1)
			ba = false;
		else
			ba = true;
		if (b % 2 == 1)
			bb = false;
		else
			bb = true;
		if (arr % 2 == 1)
			ch = false;
		else
			ch = true;
		if (arr == 0)
			heh = false;
		if (!hmm)
			cout << n;
		else
		if (ba == bb)
		{
			if (ch && heh)
				cout << n;
			else
				cout << n - 1;
		}
		else
		{
			if (!ch)
				cout << n;
			else
				cout << n - 1;
		}
		cout << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <bitset>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <unordered_map>
#include <tuple>
#include <cmath>
#include <functional>
#include <algorithm>
#include <set>
#include <deque>
#include <cassert>
#include <numeric>
using namespace std;
#define kekek ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

signed main() {
	kekek;
    int t;
    cin >> t;
    while (t--) {
        int cnt;
        cin >> cnt;

        vector<string> s(cnt);
        for (auto &i : s) {
            cin >> i;
        }
        int cnt0 = 0, cnt1 = 0, odd = 0;
        for (auto &i : s) {
            for (int j = 0; j < i.size(); j++) {
                if (i[j] == '0') cnt0++;
                else cnt1++;
            }
            if (i.size() % 2 == 1)odd++;
        }

        if ((cnt1 + cnt0) % 2 == odd % 2) {
            cout << cnt << '\n';
        }
        else {
            cout << cnt - 1 << '\n';
        }
    }

	//system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <bitset>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <unordered_map>
#include <tuple>
#include <cmath>
#include <functional>
#include <algorithm>
#include <set>
#include <deque>
#include <cassert>
#include <numeric>
using namespace std;
#define kekek ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

signed main() {
	kekek;
    int t;
    cin >> t;
    while (t--) {
        int cnt;
        cin >> cnt;

        vector<string> s(cnt);
        for (auto &i : s) {
            cin >> i;
        }
        int cnt0 = 0, cnt1 = 0, odd = 0;
        for (auto &i : s) {
            for (int j = 0; j < i.size(); j++) {
                if (i[j] == '0') cnt0++;
                else cnt1++;
            }
            if (i.size() % 2 == 1)odd++;
        }

        if (cnt1 % 2 + cnt0 % 2 == odd % 2) {
            cout << cnt << '\n';
        }
        else {
            cout << cnt - 1 << '\n';
        }
    }

//	system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <bitset>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <unordered_map>
#include <tuple>
#include <cmath>
#include <functional>
#include <algorithm>
#include <set>
#include <deque>
#include <cassert>
#include <numeric>
using namespace std;
#define kekek ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

signed main() {
	kekek;
    int t;
    cin >> t;
    while (t--) {
        int cnt;
        cin >> cnt;

        vector<string> s(cnt);
        for (auto &i : s) {
            cin >> i;
        }
        int cnt0 = 0, cnt1 = 0, odd = 0;
        for (auto &i : s) {
            for (int j = 0; j < i.size(); j++) {
                if (i[j] == '0') cnt0++;
                else cnt1++;
            }
            if (i.size() % 2 == 1)odd++;
        }

        if (odd == 0) {
            if (cnt1 % 2 == 0 && cnt0 % 2 == 0) {
                cout << cnt << '\n';
            }
            else {
                cout << cnt - 1 << '\n';
            }
        }
        else {
            if ((cnt1 + cnt0) % 2 == odd % 2) {
                cout << cnt << '\n';
            }
            else {
                cout << cnt - 1 << '\n';
            }
        }
    }

//	system("pause");
	return 0;
}


#include <stdio.h>
#include <string>
using namespace std;
int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n;
		scanf("%d", &n);
		getchar();
		bool check = false;
		int len = 0;
		int cnt1 = 0;
		for (int i = 0; i < n; i++) {
			while (true) {
				char c = getchar();
				if (c == '0' || c == '1') {
					len++;
					if (c == '1') cnt1++;
				}
				else {
					if (len % 2 == 1) check = true;
					len = 0;
					break;
				}
			}
		}
		if (check || cnt1 % 2 == 0) printf("%d\n", n);
		else printf("%d\n", n - 1);
	}
}


#include <string>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int q, n;	cin >> q;
	int ones, nulls, count;
	set<int> sizes;
	string line;

	for (int q1 = 0; q1 < q; q1++)
	{
		cin >> n;	cin.ignore();
		ones = nulls = count = 0;

		for (int i = 0; i < n; i++)
		{
			getline(cin, line);
			sizes.emplace(line.length());

			for (int j = 0; j < line.length(); j++)
				if (line[j] == '0')
					nulls++;
				else ones++;
		}

		for (auto it = sizes.begin(); it != sizes.end(); it++)
		{
			int temp = *it;
			if (temp % 2)
				if (nulls % 2)
				{
					nulls--;
					temp--;
				}
				else {
					ones--;
					temp--;
				}

			temp -= min(temp, nulls / 2 * 2);
			nulls -= min(temp, nulls / 2 * 2);

			if (temp != 0)
			{
				temp -= min(temp, ones / 2 * 2);
				ones -= min(temp, ones / 2 * 2);
			}

			if (!temp)
				count++;
		}
		cout << count << '\n';
		sizes.clear();
	}

	return 0;
}


#include <string>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int q, n;	cin >> q;
	int ones, nulls, count;
	multiset<int> sizes;
	string line;

	for (int q1 = 0; q1 < q; q1++)
	{
		cin >> n;	cin.ignore();
		ones = nulls = count = 0;

		for (int i = 0; i < n; i++)
		{
			getline(cin, line);
			sizes.emplace(line.length());

			for (int j = 0; j < line.length(); j++)
				if (line[j] == '0')
					nulls++;
				else ones++;
		}

		for (auto it = sizes.begin(); it != sizes.end(); it++)
		{
			int temp = *it;
			if (temp % 2)
				if (nulls % 2)
				{
					nulls--;
					temp--;
				}
				else {
					ones--;
					temp--;
				}

			temp -= min(temp, nulls / 2 * 2);
			nulls -= min(temp, nulls / 2 * 2);

			if (temp != 0)
			{
				temp -= min(temp, ones / 2 * 2);
				ones -= min(temp, ones / 2 * 2);
			}

			if (!temp)
				count++;
		}
		cout << count << '\n';
		sizes.clear();
	}

	return 0;
}


#include <string>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int q, n;	cin >> q;
	int ones, nulls, count;
	multiset<int> sizes;
	string line;

	for (int q1 = 0; q1 < q; q1++)
	{
		cin >> n;	cin.ignore();
		ones = nulls = count = 0;

		for (int i = 0; i < n; i++)
		{
			getline(cin, line);
			sizes.emplace(line.length());

			for (int j = 0; j < line.length(); j++)
				if (line[j] == '0')
					nulls++;
				else ones++;
		}

		//cout << nulls << ' ' << ones << '\n';

		for (auto it = sizes.begin(); it != sizes.end(); it++)
		{

			//cout << "Size is: " << *it << "\n\n";

			int temp1 = *it, temp2;
			if (temp1 % 2)
				if (nulls % 2)
				{
					nulls--;
					temp1--;
				}
				else {
					ones--;
					temp1--;
				}

			//cout << "Before: " << nulls << ' ' << ones << '\n';

			temp2 = temp1;
			temp1 -= min(temp1, (nulls / 2) * 2);
			nulls -= min(temp2, (nulls / 2) * 2);

			if (temp1 != 0)
			{
				temp2 = temp1;
				temp1 -= min(temp1, (ones / 2) * 2);
				ones -= min(temp2, (ones / 2) * 2);
			}

			if (temp1 == 0)
				count++;

			//cout << "After: " << nulls << ' ' << ones << '\n';
		}
		cout << count << '\n';
		sizes.clear();
	}

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#define ll long long

using namespace std;

int t, n, a, b, len, lenji;
string x;
int main() {
	ios::sync_with_stdio(false);
	cin >> t;
	while (t--) {
		a = 0; b = 0; lenji = 0;
		cin >> n;
		for (int i = 1; i <= n; i++) {
			cin >> x;
			len = x.size();
			if (len & 1) {
				lenji = 1;
			}
			for (int j = 0; j < len; j++) {
				if (x[j] == '0') {
					a++;
				}
				else {
					b++;
				}
			}
		}
		if (a & 1 && b & 1 && !lenji) {
			cout << n - 1 << endl;
		}
		else {
			cout << n << endl;
		}
	}
	return 0;
}


#include<stdio.h>

char s[60][60];

int main(void){
	int Q;
	scanf("%d", &Q);
	while(Q--){
		int N, cnt=0, check=0;
		scanf("%d", &N);
		for(int i=0; i<N; i++){
			scanf("%s", &s[i]);
			int j;
			for(j=0; s[i][j]; j++){
				if(s[i][j]=='1') cnt++;
			}
//			printf("%d\n", j);
			if(j%2) check=1;
		}
		if(!check) check = !(cnt%2);
		printf("%d\n", N-(1-check));
	}
}


#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>

using namespace std;

int main() {
	int q;
	cin >> q;
	while (q--) {
		int n;
		cin >> n;
		string s[50];
		int ans = 0;
		for (int i = 0; i < n; i++)
			cin >> s[i];
		int count[50][2]{};
		for (int i = 0; i < n; i++) {
			if (s[i].size() & 1) {
				continue;
			}
			for (int j = 0; j < s[i].size(); j++) {
				count[i][s[i][j] - '0']++;
			}
		}

		bool used[50]{};

		for (int i = 0; i < n; i++) {
			if (s[i].size() & 1) {
				ans++;
				continue;
			}
			if (!(count[i][0] & 1) || !(count[i][1] & 1)) {
				ans++;
				continue;
			}
			for (int j = 0; j < n; j++) {
				if (used[i] || used[j])continue;
				if (i == j)continue;
				if (s[j].size() & 1) {
					used[i] = 1;
					used[j] = 1;
					ans++;
					break;
				}
				if ((count[j][0] & 1) || (count[j][1] & 1)) {
					ans += 2;
					used[i] = 1;
					used[j] = 1;
					break;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>

using namespace std;

int main() {
	int q;
	cin >> q;
	while (q--) {
		int n;
		cin >> n;
		string s[50];
		int ans = 0;
		for (int i = 0; i < n; i++)
			cin >> s[i];
		int count[50][2]{};
		for (int i = 0; i < n; i++) {
			if (s[i].size() & 1) {
				continue;
			}
			for (int j = 0; j < s[i].size(); j++) {
				count[i][s[i][j] - '0']++;
			}
		}

		bool used[50]{};

		for (int i = 0; i < n; i++) {
			if (s[i].size() & 1) {
				ans++;
				continue;
			}
			if (!(count[i][0] & 1) || !(count[i][1] & 1)) {
				ans++;
				continue;
			}
			for (int j = 0; j < n; j++) {
				if (i == j)continue;
				if (used[j])continue;
				if (s[j].size() & 1)continue;
				if (!(count[j][0] & 1) || !(count[j][1] & 1))continue;
				if (count[j][0] & 1 || count[j][1] & 1) {
					used[i] = 1;
					used[j] = 1;
					ans++;
					break;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>

using namespace std;

int main() {
	int q;
	cin >> q;
	while (q--) {
		int n;
		cin >> n;
		string s[50];
		int ans = 0;
		for (int i = 0; i < n; i++)
			cin >> s[i];
		int count[50][2]{};
		for (int i = 0; i < n; i++) {
			if (s[i].size() & 1) {
				continue;
			}
			for (int j = 0; j < s[i].size(); j++) {
				count[i][s[i][j] - '0']++;
			}
		}

		bool used[50]{};

		for (int i = 0; i < n; i++) {
			if (s[i].size() & 1) {
				ans++;
				continue;
			}
			if (!(count[i][0] & 1) || !(count[i][1] & 1)) {
				ans++;
				continue;
			}
			for (int j = 0; j < n; j++) {
				if (used[i] || used[j])continue;
				if (i == j)continue;
				if (s[j].size() & 1) {
					used[i] = 1;
					used[j] = 1;
					ans++;
					break;
				}
				if (count[i][0] & 1 || count[i][1] & 1) {
					ans += 2;
					used[i] = 1;
					used[j] = 1;
					break;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>

using namespace std;

int main() {
	int q;
	cin >> q;
	while (q--) {
		int n;
		cin >> n;
		string s[50];
		int ans = 0;
		for (int i = 0; i < n; i++)
			cin >> s[i];
		int count[50][2]{};
		for (int i = 0; i < n; i++) {
			if (s[i].size() & 1) {
				continue;
			}
			for (int j = 0; j < s[i].size(); j++) {
				count[i][s[i][j] - '0']++;
			}
		}

		bool used[50]{};

		for (int i = 0; i < n; i++) {
			if (s[i].size() & 1) {
				ans++;
				continue;
			}
			if (!(count[i][0] & 1) || !(count[i][1] & 1)) {
				ans++;
				continue;
			}
			for (int j = 0; j < n; j++) {
				if (used[i] || used[j])continue;
				if (i == j)continue;
				if (s[j].size() & 1) {
					used[i] = 1;
					ans++;
					break;
				}
				if ((count[j][0] & 1) || (count[j][1] & 1)) {
					ans += 2;
					used[i] = 1;
					used[j] = 1;
					break;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
#include <map>
#include <queue>
#include <list>
#include <stack>
#include <set>
#include <string>

#define FOR(a, b, i) for(long long i = a; i < b; ++i)
#define RFOR(a, b, i) for(long long i = a; i > b; --i)

using namespace std;

using ll = int64_t;
using db = double;
using pll = std::pair<ll, ll>;

ll binpow(ll n, ll m)
{
	ll result = 1;
	while (m)
	{
		if (m & 1)
		{
			result *= n;
			--m;
		}
		else
		{
			n *= n;
			m >>= 1;
		}
	}
	return result;
}

int main()
{
	int q = 1;
	cin >> q;

	FOR(0, q, w)
	{
		int n;
		std::cin >> n;

		ll c0 = 0, c1 = 0;
		ll ne = 0;
		std::vector<ll> st;
		FOR(0, n, i)
		{
			std::string s;
			std::cin >> s;
			st.push_back(s.size());
			if (s.size() % 2 == 1) ne++;
			for (auto& j : s)
			{
				if (j == '0')
					++c0;
				else
					++c1;
			}
		}

		if ((c0 - ne) % 2 == 0) std::cout << n;
		else if ((c1 - ne) % 2 == 0) std::cout << n;
		else
		{

			bool flag = false;
			for (int i = 0; i < ne; ++i)
			{
				if (((c0 - i) % 2 == 0 && (c1 - ne + i) % 2 == 0) || ((c0 - ne + i) % 2 == 0 && (c1 - i) % 2 == 0))
					flag = true;
			}

			std::cout << ((flag) ? n : n - 1);
		}

		std::cout << "\n";
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>

using namespace std;

const int INF = 1000000000;

int main()
{
	long long q, n, one, zero, res;
	cin >> q;
	string str;
	string::iterator ind;
	for (int k = 0; k < q; k++) {
		res = 0;
		int palindrom = 0;
		cin >> n;
		vector <int> len;
		for (int i = 0; i < n; i++) {
			cin >> str;
			one = 0;
			zero = 0;
			if (str.length() % 2 == 1) {
				res = n;
			}
			len.push_back(str.length());
			for (int j = 0; j < str.length(); j++) {
				if (str[j] == '0')
					zero++;
				else
					one++;
			}
			if (str.length() % 2 == 0) {
				if ((zero % 2 == 0) && (one % 2 == 0)) {
					palindrom++;
				}
			}
			if (str.length() % 2 == 1) {
				if ((zero % 2 == 1) && (one % 2 == 0)) {
					palindrom++;
				}
				if ((zero % 2 == 0) && (one % 2 == 1)) {
					palindrom++;
				}
			}
		}
		if (res == 0) {
			if ((len.size() - palindrom) % 2 == 0)
				res = n;
			else {
				res = n - 1;
			}
		}
		cout << res << '\n';
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>

using namespace std;

int n;
string s;
int a, b, ans = 0;
vector<int> length;

void solve() {
    ans = 0;
    length.clear();
    a = 0; b = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> s;
        length.push_back(s.size());
        for (auto c: s) {
            if (c == '1') {
                a++;
            } else {
                b++;
            }
        }
    }
    sort(length.begin(), length.end());
    for (auto l: length) {
        int kek = l / 2 * 2;
        if (a / 2 * 2 + b / 2 * 2 < kek) {
            continue;
        }
        if (a / 2 * 2 + b / 2 * 2 + a % 2 + b % 2 < l && (l & 1)) {
            continue;
        }
        int d = min(kek, a / 2 * 2);
        a -= d;
        kek -= d;
        d = min(kek, b / 2 * 2);
        b -= d;
        kek -= b;

        if (l & 1) {
            if (a & 1) {
                a--;
                ans++;
                continue;
            }
            if (b & 1) {
                b--;
                ans++;
                continue;
            }
            if (a) {
                a--;
                ans++;
                continue;
            }
            if (b) {
                b--;
                ans++;
                continue;
            }
        }
        ans++;
    }
    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        solve();
    }
}


#include<iostream>
#include<set>
#include<bitset>
#include<algorithm>
#include<vector>
#include<utility>
#include<map>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<list>
#include<stack>
#include<functional>
#include<queue>
#include<deque>
#include<sstream>
#include<fstream>
using namespace std;
int n;
stack <string> st;
string res,s[100005];
int main()
{
	cin>>n;
	int i=0;
	while(n)
	{
		cin>>s[++i];
		if(s[i][0]=='i')
		n--;
	}
	for(;i>0;i--)
	{
		if(s[i][0]=='i')
		st.push(s[i]);
		else if(st.size()<2)
		{
			cout<<"Error occurred";
			return 0;
		}
		else{
			res="pair<";
			res+=st.top();
			st.pop();
			res+=",";
			res+=st.top();
			st.pop();
			res+=">";
			st.push(res);
		}
	}
	if(st.size()!=1)
	cout<<"Error occurred";
	else cout<<st.top();
	return 0;
}


#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
int n,k,rc;
string e=" ",t,s[10005];
stringstream out;
void dfs(int d)
{
    if(s[d]=="")
	{
	cout<<"Error occurred";
	exit(0);
	}
	out<<s[d];
	if(s[d]=="pair")
	{
	rc++;
	out<<"<";
	dfs(d+1);
	}
	else if(rc)
	{
	rc--;
	if(k%2==1)
	out<<">";
	out<<",";
	k++;
	dfs(d+1);
	}
	else out<<">";
}
int main()
{
	cin>>n;
	while(n)
	{
	cin>>t;
	if(t=="int")
	n--;
	s[++k]=t;
	}
	k=0;
	cin>>e;
	if(e=="pair"||e=="int")
	{
	cout<<"Error occurred";
	return 0;
	}
	dfs(1);
	cout<<out.str();
	return 0;
}


/**
 * Description: CF Round#120 C
 * Author: Ply_py
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#include <queue>
#include <cmath>
using namespace std;

string a, b;
bool flag;
void check() {
    if (cin >> a) {
        b += a;
        if ("pair" == a) {
            b += "<";
            check();
            b += ",";
            check();
            b += ">";
        }
    } else {
        flag = true;
    }
}
void test() {
    for (int i = 1; i <= 100; ++i)
        continue;
}
int main() {
    cin >> a;
    check();
    test();
    if (flag || cin >> a) {
        cout << "Error occurred" << endl;
    } else {
        cout << b << endl;
    }
    return 0;
}


/**
 * Description: CF Round#120 C
 * Author: Ply_py
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#include <queue>
#include <cmath>
using namespace std;

string a, b;
bool flag;
void check() {
    if (cin >> a) {
        b += a;
        if ("pair" == a) {
            b += "<";
            check();
            b += ",";
            check();
            b += ">";
        }
    } else {
        flag = true;
    }
}
int main() {
    cin >> a;
    check();
    if (flag || cin >> a) {
        cout << "Error occurred" << endl;
    } else {
        cout << b << endl;
    }
    return 0;
}


#include<stdio.h>
#include<iostream>
using namespace std;
string s,z;
int t;
void F()
{
    if(cin>>s)
    {
        z+=s;
        if(s=="pair")
            z+="<",F(),z+=",",F(),z+=">";
    }
    else
        t=1;
}
int main()
{
    cin>>s;
    F();
    if(t||cin>>s)
        puts("Error occurred");
    else
        puts(z.c_str());
    return 0;
}


/**
 * Description: CF Round#120 C
 * Author: Ply_py
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#include <queue>
#include <cmath>
using namespace std;

string a, b;
bool flag;
void check() {
    if (cin >> a) {
        b += a;
        if ("pair" == a) {
            b += "<",check(),b+=",",check(),b+=">";
//            b += "<";
//            check();
//            b += ",";
//            check();
//            b += ">";
        }
    } else {
        flag = true;
    }
}
int main() {
    cin >> a;
    check();
    if (flag || cin >> a) {
        cout << "Error occured" << endl;
    } else {
        cout << b << endl;
    }
    return 0;
}


/**
 * Description: CF Round#120 C
 * Author: Ply_py
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#include <queue>
#include <cmath>
using namespace std;

string a, b;
bool flag;
void check() {
    if (cin >> a) {
        b = b+a;
        if ("pair" == a) {
            b += "<";
            check();
            b += ",";
            check();
            b += ">";
        }
    } else {
        flag = true;
    }
}
int main() {
    cin >> a;
    check();
    if (flag || cin >> a) {
        cout << "Error occured" << endl;
    } else {
        cout << b << endl;
    }
    return 0;
}


/**
 * Description: CF Round#120 C
 * Author: Ply_py
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#include <queue>
#include <cmath>
using namespace std;

string a, b;
bool flag;
void check() {
    if (cin >> a) {
        b = b+a;
        if ("pair" == s) {
            b += "<";
            check();
            b += ",";
            check();
            b += ">";
    } else {
        flag = true;
    }
}
int main() {
    cin >> a;
    check();
    if (t || cin >> a) {
        cout << "Error occured" << endl;
    } else {
        cout << b << endl;
    }
    return 0;
}


/**
 * Description: CF Round#120 C
 * Author: Ply_py
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#include <queue>
#include <cmath>
using namespace std;

string a, b;
bool flag;
void check() {
    if (cin >> a) {
        b += a;
        if (a == "pair") {
            b += "<",check(),b+=",",check(),b+=">";
//            b += "<";
//            check();
//            b += ",";
//            check();
//            b += ">";
        }
    } else {
        flag = true;
    }
}
int main() {
    cin >> a;
    check();
    if (flag || cin >> a) {
        cout << "Error occured" << endl;
    } else {
        cout << b << endl;
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int INF=1<<29,maxn=105;
int n,m;
char s[100005][10];
int st[100005];
struct node
{
    int r,l,k;
}nd[100005];
void dfs(int u)
{
    if(nd[u].l==0)
    {
        printf("%s",s[u]);
        return ;
    }
    printf("%s<",s[u]);
    dfs(nd[u].l);
    printf(",");
    dfs(nd[u].r);
    printf(">");
}
int main()
{
    int r,cnt,flag;
    while(~scanf("%d",&n))
    {
        r=cnt=0,flag=0;
        for(int i=1;1;i++)
        {
            scanf("%s",s[i]);
            if(i==1&&strcmp(s[i],"int")==0)
            {
                flag=1;
            }
            if(flag)continue;
            st[r++]=i;
            nd[i].k=nd[i].l=nd[i].r=0;
            if(strcmp(s[i],"int")==0)
            {
                while(r>=3)
                {
                    if(nd[st[r-1]].k!=1&&nd[st[r-2]].k!=1&&nd[st[r-3]].k==1)
                    {
                        nd[st[r-3]].l=st[r-2];nd[st[r-3]].r=st[r-1];
                        nd[st[r-3]].k=0;
                        r-=2;
                    }
                    else
                        break;
                }
                cnt++;
            }
            else nd[i].k=1;
            if(cnt==n)break;
        }
        if(r!=1||flag)puts("Error occurred");
        else
        {
            dfs(st[r-1]);
            puts("");
        }
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int INF=1<<29,maxn=105;
int n,m;
char s[100005][10];
int st[100005];
struct node
{
    int r,l,k;
}nd[100005];
void dfs(int u)
{
    if(nd[u].l==0)
    {
        printf("%s",s[u]);
        return ;
    }
    printf("%s<",s[u]);
    dfs(nd[u].l);
    printf(",");
    dfs(nd[u].r);
    printf(">");
}
int main()
{
    int r,cnt,flag;
    while(~scanf("%d",&n))
    {
        r=cnt=flag=0;
        for(int i=1;1;i++)
        {
            scanf("%s",s[i]);
            if(i==1&&strcmp(s[i],"int")==0)
            {
                flag=1;
            }
            st[r++]=i;
            nd[i].k=nd[i].l=nd[i].r=0;
            if(strcmp(s[i],"int")==0)
            {
                while(r>=3)
                {
                    if(nd[st[r-1]].k!=1&&nd[st[r-2]].k!=1&&nd[st[r-3]].k==1)
                    {
                        nd[st[r-3]].l=st[r-2];nd[st[r-3]].r=st[r-1];
                        nd[st[r-3]].k=0;
                        r-=2;
                    }
                    else
                        break;
                }
                cnt++;
            }
            else nd[i].k=1;
            if(cnt==n)break;
        }
        if(flag||r!=1)puts("Error occurred");
        else
        {
            dfs(st[r-1]);
            puts("");
        }
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int INF=1<<29,maxn=105;
int n,m;
char s[1000005][10];
int st[1000005];
struct node
{
    int r,l,k;
}nd[1000005];
void dfs(int u)
{
    if(nd[u].l==0)
    {
        printf("%s",s[u]);
        return ;
    }
    printf("%s<",s[u]);
    dfs(nd[u].l);
    printf(",");
    dfs(nd[u].r);
    printf(">");
}
int main()
{
    int r,cnt,p,flag;
    while(~scanf("%d",&n))
    {
        r=cnt=p=flag=0;
        for(int i=1;1;i++)
        {
            scanf("%s",s[i]);
            st[r++]=i;
            nd[i].k=nd[i].l=nd[i].r=0;
            if(strcmp(s[i],"int")==0)
            {
                while(r>=3)
                {
                    if(nd[st[r-1]].k!=1&&nd[st[r-2]].k!=1&&nd[st[r-3]].k==1)
                    {
                        nd[st[r-3]].l=st[r-2];nd[st[r-3]].r=st[r-1];
                        nd[st[r-3]].k=0;
                        r-=2;
                    }
                    else
                    {
                        p++;
                        break;
                    }
                }
                cnt++;
            }
            else nd[i].k=1;
            if(cnt==n)break;
        }
        if(r!=1)puts("Error occurred");
        else
        {
            dfs(st[r-1]);
            puts("");
        }
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int INF=1<<29,maxn=105;
int n,m;
char s[1000005][10];
int st[1000005];
struct node
{
    int r,l,k;
}nd[1000005];
void dfs(int u)
{
    if(nd[u].l==0)
    {
        printf("%s",s[u]);
        return ;
    }
    printf("%s<",s[u]);
    dfs(nd[u].l);
    printf(",");
    dfs(nd[u].r);
    printf(">");
}
int main()
{
    int r,cnt,p,flag,len;
    while(~scanf("%d",&n))
    {
        r=cnt=p=flag=len=0;
        for(len=1;1;len++)
        {
            scanf("%s",s[len]);
            if(strcmp(s[len],"int")==0)
                cnt++;
            else p++;
            if(cnt==n)break;
        }
        if((strcmp(s[1],"int")==0&&p!=0)||p!=n-1)
        {
            puts("Error occurred");
            break;
        }
        for(int i=1;i<=len;i++)
        {
            st[r++]=i;
            nd[i].k=nd[i].l=nd[i].r=0;
            if(strcmp(s[i],"int")==0)
            {
                while(r>=3)
                {
                    if(nd[st[r-1]].k!=1&&nd[st[r-2]].k!=1&&nd[st[r-3]].k==1)
                    {
                        nd[st[r-3]].l=st[r-2];nd[st[r-3]].r=st[r-1];
                        nd[st[r-3]].k=0;
                        r-=2;
                    }
                    else
                        break;
                }
            }
            else nd[i].k=1;
        }
        if(r!=1)puts("Error occurred");
        else
        {
            dfs(st[r-1]);
            puts("");
        }
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int INF=1<<29,maxn=105;
int n,m;
char s[100005][10];
int st[100005];
struct node
{
    int r,l,k;
}nd[100005];
void dfs(int u)
{
    if(nd[u].l==0)
    {
        printf("%s",s[u]);
        return ;
    }
    printf("%s<",s[u]);
    dfs(nd[u].l);
    printf(",");
    dfs(nd[u].r);
    printf(">");
}
int main()
{
    int r,cnt;
    while(~scanf("%d",&n))
    {
        r=cnt=0;
        for(int i=1;1;i++)
        {
            scanf("%s",s[i]);
            st[r++]=i;
            nd[i].k=nd[i].l=nd[i].r=0;
            if(strcmp(s[i],"int")==0)
            {
                while(r>=3)
                {
                    if(nd[st[r-1]].k!=1&&nd[st[r-2]].k!=1&&nd[st[r-3]].k==1)
                    {
                        nd[st[r-3]].l=st[r-2];nd[st[r-3]].r=st[r-1];
                        nd[st[r-3]].k=0;
                        r-=2;
                    }
                    else
                        break;
                }
                cnt++;
            }
            else nd[i].k=1;
            if(cnt==n)break;
        }
        if(r!=1)puts("Error occurred");
        else
        {
            dfs(st[r-1]);
            puts("");
        }
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int INF=1<<29,maxn=105;
int n,m;
string st[100005];
int main()
{
    string in,out;
    int r,f;r=f=0;
    int flag;
    while(~scanf("%d",&n))
    {
        int cnt=0;
        flag=0;
        while(1)
        {
            cin>>in;
            if(in=="int")
            {
                if(r>=2&&st[r-1]!="pair"&&st[r-2]=="pair")
                {
                    out="";
                    out=st[r-2];
                    out+='<';
                    out+=st[r-1];
                    out+=',';
                    out+=in;
                    out+='>';
                    r-=2;
                    st[r++]=out;
                }
                else if(r>=1&&st[r-1]=="pair")
                    st[r++]=in;
                else flag=1;
                cnt++;
            }
            else
            {
                st[r++]=in;
            }
            if(cnt==n)break;
        }
        while(r>=3)
        {
            if(st[r-1]!="pair"&&st[r-2]!="pair"&&st[r-3]=="pair")
            {
                    out="";
                    out=st[r-3];
                    out+='<';
                    out+=st[r-2];
                    out+=',';
                    out+=st[r-1];
                    out+='>';
                    r-=3;
                    st[r++]=out;
            }
            else
                break;
        }
        if(flag==1||r!=1)puts("Error occurred");
        else cout<<st[r-1]<<endl;
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int INF=1<<29,maxn=105;
int n,m;
char s[100005][10];
int st[100005];
struct node
{
    int r,l,k;
}nd[100005];
void dfs(int u)
{
    if(nd[u].l==0)
    {
        printf("%s",s[u]);
        return ;
    }
    printf("%s<",s[u]);
    dfs(nd[u].l);
    printf(",");
    dfs(nd[u].r);
    printf(">");
}
int main()
{
    int r,cnt,p,flag;
    while(~scanf("%d",&n))
    {
        r=cnt=p=flag=0;
        for(int i=1;1;i++)
        {
            scanf("%s",s[i]);
            st[r++]=i;
            nd[i].k=nd[i].l=nd[i].r=0;
            if(strcmp(s[i],"int")==0)
            {
                while(r>=3)
                {
                    if(nd[st[r-1]].k!=1&&nd[st[r-2]].k!=1&&nd[st[r-3]].k==1)
                    {
                        nd[st[r-3]].l=st[r-2];nd[st[r-3]].r=st[r-1];
                        nd[st[r-3]].k=0;
                        r-=2;
                    }
                    else
                    {
                        p++;
                        break;
                    }
                }
                cnt++;
            }
            else nd[i].k=1;
            if(cnt==n)break;
        }
        if(p!=r-1||r!=1||(p!=0&&strcmp(s[1],"int")))puts("Error occurred");
        else
        {
            dfs(st[r-1]);
            puts("");
        }
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int INF=1<<29,maxn=105;
int n,m;
char s[1000005][10];
int st[1000005];
struct node
{
    int r,l,k;
}nd[1000005];
void dfs(int u)
{
    if(nd[u].l==0)
    {
        printf("%s",s[u]);
        return ;
    }
    printf("%s<",s[u]);
    dfs(nd[u].l);
    printf(",");
    dfs(nd[u].r);
    printf(">");
}
int main()
{
    int r,cnt,p,flag,len;
    scanf("%d",&n);
    len=1;
    while(~scanf("%s",s[len]))
    {
        len++;
    }
    r=cnt=p=0;
    for(int i=1;i<len;i++)
    {
        st[r++]=i;
        nd[i].k=nd[i].l=nd[i].r=0;
        if(strcmp(s[i],"int")==0)
        {
            while(r>=3)
            {
                if(nd[st[r-1]].k!=1&&nd[st[r-2]].k!=1&&nd[st[r-3]].k==1)
                {
                    nd[st[r-3]].l=st[r-2];nd[st[r-3]].r=st[r-1];
                    nd[st[r-3]].k=0;
                    r-=2;
                }
                else
                    break;
            }
        }
        else nd[i].k=1;
    }
    if(r!=1)puts("Error occurred");
    else
    {
        dfs(st[r-1]);
        puts("");
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int INF=1<<29,maxn=105;
int n,m;
char s[100005][10];
int st[100005];
struct node
{
    int r,l,k;
}nd[100005];
void dfs(int u)
{
    if(nd[u].l==0)
    {
        printf("%s",s[u]);
        return ;
    }
    printf("%s<",s[u]);
    dfs(nd[u].l);
    printf(",");
    dfs(nd[u].r);
    printf(">");
}
int main()
{
    int r,cnt,p,flag;
    while(~scanf("%d",&n))
    {
        r=cnt=p=flag=0;
        for(int i=1;1;i++)
        {
            scanf("%s",s[i]);
            st[r++]=i;
            nd[i].k=nd[i].l=nd[i].r=0;
            if(strcmp(s[i],"int")==0)
            {
                while(r>=3)
                {
                    if(nd[st[r-1]].k!=1&&nd[st[r-2]].k!=1&&nd[st[r-3]].k==1)
                    {
                        nd[st[r-3]].l=st[r-2];nd[st[r-3]].r=st[r-1];
                        nd[st[r-3]].k=0;
                        r-=2;
                    }
                    else
                    {
                        p++;
                        break;
                    }
                }
                cnt++;
            }
            else nd[i].k=1;
            if(cnt==n)break;
        }
        if(r!=1)puts("Error occurred");
        else
        {
            dfs(st[r-1]);
            puts("");
        }
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int INF=1<<29,maxn=105;
int n,m;
stack< string > st;
int main()
{
    string in,out,tem;
    int r,f;r=f=0;
    int flag;
    while(~scanf("%d",&n))
    {
        int cnt=0;
        flag=0;
        while(1)
        {
            cin>>in;
            if(in=="int")
            {
                st.push(in);
                while(st.size()>=3)
                {
                    if(st.top()!="pair")
                    {
                        in=st.top();
                        st.pop();
                        if(st.top()!="pair")
                        {
                            out=st.top();
                            st.pop();
                            if(st.top()=="pair")
                            {
                                tem=st.top();
                                st.pop();
                                tem+='<';
                                tem+=out;
                                tem+=',';
                                tem+=in;
                                tem+='>';
                                st.push(tem);
                            }
                            else
                            {
                                flag=1;
                                break;
                            }
                        }
                        else
                        {
                            st.push(in);
                            break;
                        }
                    }
                    else break;
                }
                cnt++;
            }
            else
            {
                st.push(in);
            }

            if(cnt==n)break;
        }
        if(flag==1||st.size()!=1)puts("Error occurred");
        else cout<<st.top()<<endl;
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int INF=1<<29,maxn=105;
int n,m;
string st[100005];
int main()
{
    string in,out;
    int r,f;r=f=0;
    int flag;
    while(~scanf("%d",&n))
    {
        int cnt=0;
        flag=0;
        while(1)
        {
            cin>>in;
            if(in=="int")
            {
                if(r>=2&&st[r-1]!="pair"&&st[r-2]=="pair")
                {
                    out="";
                    out=st[r-2];
                    out+='<';
                    out+=st[r-1];
                    out+=',';
                    out+=in;
                    out+='>';
                    r-=2;
                    st[r++]=out;
                    while(r>=3)
                    {
                        if(st[r-1]!="pair"&&st[r-2]!="pair"&&st[r-3]=="pair")
                        {
                            out="";
                            out=st[r-3];
                            out+='<';
                            out+=st[r-2];
                            out+=',';
                            out+=st[r-1];
                            out+='>';
                            r-=3;
                            st[r++]=out;
                        }
                        else
                            break;
                    }
                }
                else if(r>=1&&st[r-1]=="pair")
                    st[r++]=in;
                else flag=1;
                cnt++;
            }
            else
            {
                st[r++]=in;
            }
            if(cnt==n)break;
        }
        while(r>=3)
        {
            if(st[r-1]!="pair"&&st[r-2]!="pair"&&st[r-3]=="pair")
            {
                out="";
                out=st[r-3];
                out+='<';
                out+=st[r-2];
                out+=',';
                out+=st[r-1];
                out+='>';
                r-=3;
                st[r++]=out;
            }
            else
                break;
        }
        if(flag==1||r!=1)puts("Error occurred");
        else cout<<st[r-1]<<endl;
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int INF=1<<29,maxn=105;
int n,m;
char s[1000005][10];
int st[1000005];
struct node
{
    int r,l,k;
}nd[1000005];
void dfs(int u)
{
    if(nd[u].l==0)
    {
        printf("%s",s[u]);
        return ;
    }
    printf("%s<",s[u]);
    dfs(nd[u].l);
    printf(",");
    dfs(nd[u].r);
    printf(">");
}
int main()
{
    int r,cnt,p,flag;
    while(~scanf("%d",&n))
    {
        r=cnt=p=flag=0;
        for(int i=1;1;i++)
        {
            scanf("%s",s[i]);
            st[r++]=i;
            nd[i].k=nd[i].l=nd[i].r=0;
            if(strcmp(s[i],"int")==0)
            {
                while(r>=3)
                {
                    if(nd[st[r-1]].k!=1&&nd[st[r-2]].k!=1&&nd[st[r-3]].k==1)
                    {
                        nd[st[r-3]].l=st[r-2];nd[st[r-3]].r=st[r-1];
                        nd[st[r-3]].k=0;
                        r-=2;
                    }
                    else
                        break;
                }
                cnt++;
            }
            else p++,nd[i].k=1;
            if(cnt==n)break;
        }
        if(strcmp(s[1],"int")&&p!=0||r!=1||p!=n-1)puts("Error occurred");
        else
        {
            dfs(st[r-1]);
            puts("");
        }
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
const int INF=1<<29,maxn=105;
int n,m;
char s[1000005][10];
int st[1000005];
struct node
{
    int r,l,k;
}nd[1000005];
void dfs(int u)
{
    if(nd[u].l==0)
    {
        printf("%s",s[u]);
        return ;
    }
    printf("%s<",s[u]);
    dfs(nd[u].l);
    printf(",");
    dfs(nd[u].r);
    printf(">");
}
int main()
{
    int r,cnt,p,flag;
    while(~scanf("%d",&n))
    {
        r=cnt=p=flag=0;
        for(int i=1;1;i++)
        {
            scanf("%s",s[i]);
            st[r++]=i;
            nd[i].k=nd[i].l=nd[i].r=0;
            if(strcmp(s[i],"int")==0)
            {
                while(r>=3)
                {
                    if(nd[st[r-1]].k!=1&&nd[st[r-2]].k!=1&&nd[st[r-3]].k==1)
                    {
                        nd[st[r-3]].l=st[r-2];nd[st[r-3]].r=st[r-1];
                        nd[st[r-3]].k=0;
                        r-=2;
                    }
                    else
                        break;
                }
                cnt++;
            }
            else p++,nd[i].k=1;
            if(cnt==n)break;
        }
        if(strcmp(s[1],"int")==0&&p!=0||r!=1||p!=n-1)puts("Error occurred");
        else
        {
            dfs(st[r-1]);
            puts("");
        }
    }

    return 0;
}


#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

typedef pair<int, int> pii;

int n;
string s;
stringstream ss;
bool arr[100005];
pii res[100005];

void print(int i){
    if(res[i].first<0) cout << "int";
    else {
        cout << "pair<";
        print(res[i].first);
        cout << ",";
        print(res[i].second);
        cout << ">";
    }
}

int main(){

    ios::sync_with_stdio(false);
    getline(cin, s);
    getline(cin, s);
    ss << s;
    for(n = 0; !ss.eof(); n++){
        ss >> s;
        arr[n] = (s=="pair");
    }

    vector<int> v;
    for(int i = n-1; i>=0; i--){
        if(arr[i]){
            if(v.size()<2){
                cout << "Error occurred" << endl;
                return 0;
            }
            res[i] = pii(0, 0);
            res[i].first = v.back();
            v.pop_back();
            res[i].second = v.back();
            v.pop_back();
        } else res[i] = pii(-1, -1);
        v.push_back(i);
    }

    if(v.size()>1){
        cout << "Error occurred" << endl;
        return 0;
    }

    print(0);
    cout << endl;

}


#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

typedef pair<int, int> pii;

int n;
string s;
stringstream ss;
bool arr[100005];
pii res[100005];

void print(int i){
    if(res[i].first<0) cout << "int";
    else {
        cout << "pair<";
        print(res[i].first);
        cout << ",";
        print(res[i].second);
        cout << ">";
    }
}

int main(){

    ios::sync_with_stdio(false);
    getline(cin, s);
    getline(cin, s);
    ss << s;
    for(n = 0; !ss.eof(); n++){
        ss >> s;
        arr[n] = (s=="pair");
    }

    vector<int> v;
    for(int i = n-1; i>=0; i--){
        if(arr[i]){
            if(v.size()<2){
                cout << "Error occurred" << endl;
                return 0;
            }
            res[i] = pii(0, 0);
            res[i].first = v.back();
            v.pop_back();
            res[i].second = v.back();
            v.pop_back();
        } else res[i] = pii(-1, -1);
        v.push_back(i);
    }

    print(0);
    cout << endl;

}


#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int n;
string s, s1, res;
stringstream ss;
pair<int, string> arr[100005];

int main(){

    getline(cin, s);
    getline(cin, s);
    ss << s;
    for(n = 0; !ss.eof(); n++){
        ss >> s;
        arr[n] = make_pair(s=="pair",s);
    }

    vector<pair<int, string> > v;
    for(int i = n-1; i>=0; i--){
        if(arr[i].first){
            if(v.size()<2){
                cout << "Error occurred" << endl;
                return 0;
            }
            arr[i].second = "pair<"+v.back().second+",";
            v.pop_back();
            arr[i].second+=v.back().second+">";
            v.pop_back();
            v.push_back(arr[i]);
        arr[i].second = "";
        } else v.push_back(arr[i]);
    }
    cout << v[0].second << endl;

}


#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int n;
string s, s1, res;
stringstream ss;
pair<int, string> arr[100005];

int main(){

    getline(cin, s);
    getline(cin, s);
    ss << s;
    for(n = 0; !ss.eof(); n++){
        ss >> s;
        arr[n] = make_pair(s=="pair",s);
    }

    vector<pair<int, string> > v;
    for(int i = n-1; i>=0; i--){
        if(arr[i].first){
            if(v.size()<2){
                cout << "Error occurred" << endl;
                return 0;
            }
            arr[i].second = "pair<"+v.back().second+",";
            v.pop_back();
            arr[i].second+=v.back().second+">";
            v.pop_back();
            v.push_back(arr[i]);
        } else v.push_back(arr[i]);
    }
    cout << v[0].second << endl;

}


#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int n;
string s, s1, res;
stringstream ss;
pair<int, string> arr[100005];

int main(){

    ios::sync_with_stdio(false);
    getline(cin, s);
    getline(cin, s);
    ss << s;
    for(n = 0; !ss.eof(); n++){
        ss >> s;
        arr[n] = make_pair(s=="pair",s);
    }

    vector<pair<int, string> > v;
    for(int i = n-1; i>=0; i--){
        if(arr[i].first){
            if(v.size()<2){
                cout << "Error occurred" << endl;
                return 0;
            }
            arr[i].second = "pair<"+v.back().second+",";
            v.pop_back();
            arr[i].second+=v.back().second+">";
            v.pop_back();
            v.push_back(arr[i]);
            arr[i].second = "";
        } else v.push_back(arr[i]);
    }
    cout << v[0].second << endl;

}


#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int n;
string s, s1, res;
stringstream ss;
pair<int, string> arr[100005];

int main(){

    getline(cin, s);
    getline(cin, s);
    ss << s;
    for(n = 0; !ss.eof(); n++){
        ss >> s;
        arr[n] = make_pair(s=="pair",s);
    }

    if(n>10000) return 0;
    vector<pair<int, string> > v;
    for(int i = n-1; i>=0; i--){
        if(arr[i].first){
            if(v.size()<2){
                cout << "Error occurred" << endl;
                return 0;
            }
            arr[i].second = "pair<"+v.back().second+",";
            v.pop_back();
            arr[i].second+=v.back().second+">";
            v.pop_back();
            v.push_back(arr[i]);
        } else v.push_back(arr[i]);
    }
    cout << v[0].second << endl;

}


#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

typedef pair<int, int> pii;

int n;
string s;
stringstream ss;
bool arr[100005];
pii res[100005];

string print(int i){
    if(res[i].first<0) return "int";
    else return "pair<"+print(res[i].first)+","+print(res[i].second)+">";
}

int main(){

    ios::sync_with_stdio(false);
    getline(cin, s);
    getline(cin, s);
    ss << s;
    for(n = 0; !ss.eof(); n++){
        ss >> s;
        arr[n] = (s=="pair");
    }

    vector<int> v;
    for(int i = n-1; i>=0; i--){
        if(arr[i]){
            if(v.size()<2){
                cout << "Error occurred" << endl;
                return 0;
            }
            res[i] = pii(0, 0);
            res[i].first = v.back();
            v.pop_back();
            res[i].second = v.back();
            v.pop_back();
        } else res[i] = pii(-1, -1);
        v.push_back(i);
    }

    cout << print(0) << endl;

}


#include "assert.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include <stack>
#include <queue>
using namespace std;
#define LIMIT (100000+4)
#define DEBUG 0
int main()
{
   if (DEBUG) freopen("stl.01.in", "r", stdin);
   int n; int angles = 0; int count = 0; stack<char> S; queue<char> Q;
   char s[LIMIT][8] = {0};
   scanf("%d", &n); assert(n>0 && n<LIMIT);
   for (int jack = 0; jack < LIMIT; jack++) if (scanf("%s", &s[jack]) == 0) break;
   if (DEBUG) { for (int jack = 0; jack < LIMIT && count != -1; jack++) if (s[jack][0] == 0) break; else printf("%s ", s[jack]); printf("\n"); }
   for (int jack = 0; jack < LIMIT; jack++)
      if (s[jack][0] == 0) break;
      else if (!strcmp("pair", s[jack])) {
         S.push('P'); S.push('B'); angles++;
         Q.push('P'); Q.push('B');
      } else if (!strcmp("int", s[jack])) {
         if (S.empty()) {
            count++; Q.push('I'); if (s[jack+1][0]!=0) S.push('I'); break;
         } else if (S.top() == 'B') {
            S.push('I'); S.push('C'); count++;
            Q.push('I'); Q.push('C');
         } else if (S.top() == 'C') {
            S.push('I'); S.push('E'); count++; angles--;
            Q.push('I'); Q.push('E');
            while (!S.empty() && S.top()=='E') {
               S.pop(); while (!S.empty() && S.top()!='P') S.pop(); S.pop();
               if (S.empty()) continue;
               if (S.top() == 'B') { S.push('I'); S.push('C'); Q.push('C'); }
               else if (S.top() == 'C') { S.push('I'); S.push('E'); angles--; Q.push('E'); } else assert(0);
            }
            if (S.empty() && s[jack+1][0]!=0) break;
         }
      }
   if (!S.empty() || count!=n || angles!=0) printf("%s", "Error occurred");
   else while (!Q.empty()) {
      switch (Q.front()) {
         case 'P' : printf("%s", "pair"); break;
         case 'I' : printf("%s", "int"); break;
         case 'B' : printf("%s", "<"); break;
         case 'E' : printf("%s", ">"); break;
         case 'C' : printf("%s", ","); break;
      }
      Q.pop();
   }
   printf("\n");
   if (DEBUG) fclose(stdin);
   return 0;
}


#include "assert.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include <stack>
#include <queue>
using namespace std;
#define LIMIT (100000+4)
#define DEBUG 0
int main()
{
   if (DEBUG) freopen("stl.01.in", "r", stdin);
   int n; int angles = 0; int count = 0; stack<char> S; queue<char> Q;
   char s[LIMIT][8] = {0};
   scanf("%d", &n); assert(n>0 && n<LIMIT);
   for (int jack = 0; jack < LIMIT; jack++) if (scanf("%s", &s[jack]) == 0) break;
   if (DEBUG) { for (int jack = 0; jack < LIMIT && count != -1; jack++) if (s[jack][0] == 0) break; else printf("%s ", s[jack]); printf("\n"); }
   for (int jack = 0; jack < LIMIT; jack++)
      if (s[jack][0] == 0) break;
      else if (!strcmp("pair", s[jack])) {
         S.push('P'); S.push('B'); angles++;
         Q.push('P'); Q.push('B');
      } else if (!strcmp("int", s[jack])) {
         if (S.empty()) { count++; Q.push('I'); break; }
         else if (S.top() == 'B') {
            S.push('I'); S.push('C'); count++;
            Q.push('I'); Q.push('C');
         } else if (S.top() == 'C') {
            S.push('I'); S.push('E'); count++; angles--;
            Q.push('I'); Q.push('E');
            while (!S.empty() && S.top()=='E') {
               S.pop(); while (!S.empty() && S.top()!='P') S.pop(); S.pop();
               if (S.empty()) continue;
               if (S.top() == 'B') { S.push('I'); S.push('C'); Q.push('C'); }
               else if (S.top() == 'C') { S.push('I'); S.push('E'); angles--; Q.push('E'); } else assert(0);
            }
            if (S.empty() && s[jack+1][0]!=0) break;
         }
      }
   if (!S.empty() || count!=n || angles!=0) printf("%s", "Error occurred");
   else while (!Q.empty()) {
      switch (Q.front()) {
         case 'P' : printf("%s", "pair"); break;
         case 'I' : printf("%s", "int"); break;
         case 'B' : printf("%s", "<"); break;
         case 'E' : printf("%s", ">"); break;
         case 'C' : printf("%s", ","); break;
      }
      Q.pop();
   }
   printf("\n");
   if (DEBUG) fclose(stdin);
   return 0;
}


#include "assert.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include <stack>
#include <queue>
using namespace std;
#define LIMIT (100000+4)
#define DEBUG 0
int main()
{
   if (DEBUG) freopen("stl.01.in", "r", stdin);
   int n; int angles = 0; int count = 0; stack<char> S; queue<char> Q;
   char s[LIMIT][8] = {0};
   scanf("%d", &n); assert(n>0 && n<LIMIT);
   for (int jack = 0; jack < LIMIT; jack++) if (scanf("%s", &s[jack]) == 0) break;
   if (DEBUG) { for (int jack = 0; jack < LIMIT && count != -1; jack++) if (s[jack][0] == 0) break; else printf("%s ", s[jack]); printf("\n"); }
   for (int jack = 0; jack < LIMIT; jack++)
      if (s[jack][0] == 0) break;
      else if (!strcmp("pair", s[jack])) {
         S.push('P'); S.push('B'); angles++;
         Q.push('P'); Q.push('B');
      } else if (!strcmp("int", s[jack])) {
         if (S.empty()) { count++; break; }
         else if (S.top() == 'B') {
            S.push('I'); S.push('C'); count++;
            Q.push('I'); Q.push('C');
         } else if (S.top() == 'C') {
            S.push('I'); S.push('E'); count++; angles--;
            Q.push('I'); Q.push('E');
            while (!S.empty() && S.top()=='E') {
               S.pop(); while (!S.empty() && S.top()!='P') S.pop(); S.pop();
               if (S.empty()) continue;
               if (S.top() == 'B') { S.push('I'); S.push('C'); Q.push('C'); }
               else if (S.top() == 'C') { S.push('I'); S.push('E'); angles--; Q.push('E'); } else assert(0);
            }
            if (S.empty() && s[jack+1][0]!=0) break;
         }
      }
   if (!S.empty() || count!=n || angles!=0) printf("%s", "Error occurred");
   else while (!Q.empty()) {
      switch (Q.front()) {
         case 'P' : printf("%s", "pair"); break;
         case 'I' : printf("%s", "int"); break;
         case 'B' : printf("%s", "<"); break;
         case 'E' : printf("%s", ">"); break;
         case 'C' : printf("%s", ","); break;
      }
      Q.pop();
   }
   printf("\n");
   if (DEBUG) fclose(stdin);
   return 0;
}


#include "assert.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include <stack>
#include <queue>
using namespace std;
#define LIMIT (100000+4)
#define DEBUG 0
int main()
{
   if (DEBUG) freopen("stl.01.in", "r", stdin);
   int n; int angles = 0; int count = 0; stack<char> S; queue<char> Q;
   char s[LIMIT][8] = {0};
   scanf("%d", &n); assert(n>0 && n<LIMIT);
   for (int jack = 0; jack < LIMIT; jack++) if (scanf("%s", &s[jack]) == 0) break;
   if (DEBUG) { for (int jack = 0; jack < LIMIT && count != -1; jack++) if (s[jack][0] == 0) break; else printf("%s ", s[jack]); printf("\n"); }
   for (int jack = 0; jack < LIMIT; jack++)
      if (s[jack][0] == 0) break;
      else if (!strcmp("pair", s[jack])) {
         S.push('P'); S.push('B'); angles++;
         Q.push('P'); Q.push('B');
      } else if (!strcmp("int", s[jack])) {
         if (S.empty()) { count++; Q.push('I'); }
         else if (S.top() == 'B') {
            S.push('I'); S.push('C'); count++;
            Q.push('I'); Q.push('C');
         } else if (S.top() == 'C') {
            S.push('I'); S.push('E'); count++; angles--;
            Q.push('I'); Q.push('E');
            while (!S.empty() && S.top()=='E') {
               S.pop(); while (!S.empty() && S.top()!='P') S.pop(); S.pop();
               if (S.empty()) continue;
               if (S.top() == 'B') { S.push('I'); S.push('C'); Q.push('C'); }
               else if (S.top() == 'C') { S.push('I'); S.push('E'); angles--; Q.push('E'); } else assert(0);
            }
            if (S.empty() && s[jack+1][0]!=0) break;
         }
      }
   if (!S.empty() || count!=n || angles!=0) printf("%s", "Error occurred");
   else while (!Q.empty()) {
      switch (Q.front()) {
         case 'P' : printf("%s", "pair"); break;
         case 'I' : printf("%s", "int"); break;
         case 'B' : printf("%s", "<"); break;
         case 'E' : printf("%s", ">"); break;
         case 'C' : printf("%s", ","); break;
      }
      Q.pop();
   }
   printf("\n");
   if (DEBUG) fclose(stdin);
   return 0;
}


#include "assert.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include <stack>
#include <queue>
using namespace std;
#define LIMIT (100000+4)
#define DEBUG 0
int main()
{
   if (DEBUG) freopen("stl.02.in", "r", stdin);
   int n; int angles = 0; int count = 0; stack<char> S; queue<char> Q;
   char s[LIMIT][8] = {0};
   scanf("%d", &n); assert(n>0 && n<LIMIT);
   for (int jack = 0; jack < LIMIT; jack++) if (scanf("%s", &s[jack]) == 0) break;
   if (DEBUG) for (int jack = 0; jack < LIMIT && count != -1; jack++) if (s[jack][0] == 0) break; else printf("%s ", s[jack]); printf("\n");
   for (int jack = 0; jack < LIMIT; jack++)
      if (s[jack][0] == 0) break;
      else if (!strcmp("pair", s[jack])) {
         S.push('P'); S.push('B'); angles++;
         Q.push('P'); Q.push('B');
      } else if (!strcmp("int", s[jack])) {
         if (S.empty()) { count++; break; }
         else if (S.top() == 'B') {
            S.push('I'); S.push('C'); count++;
            Q.push('I'); Q.push('C');
         } else if (S.top() == 'C') {
            S.push('I'); S.push('E'); count++; angles--;
            Q.push('I'); Q.push('E');
            while (!S.empty() && S.top()=='E') {
               S.pop(); while (!S.empty() && S.top()!='P') S.pop(); S.pop();
               if (S.empty()) continue;
               if (S.top() == 'B') { S.push('I'); S.push('C'); }
               else if (S.top() == 'C') { S.push('I'); S.push('E'); angles--; } else assert(0);
            }
            if (S.empty() && s[jack+1][0]!=0) break;
         }
      }
   if (!S.empty() || count!=n) printf("%s", "Error occurred");
   else while (!Q.empty()) {
      switch (Q.front()) {
         case 'P' : printf("%s", "pair"); break;
         case 'I' : printf("%s", "int"); break;
         case 'B' : printf("%s", "<"); break;
         case 'E' : printf("%s", ">"); break;
         case 'C' : printf("%s", ","); break;
      }
      Q.pop();
   }
   printf("\n");
   if (DEBUG) fclose(stdin);
   return 0;
}


#include "assert.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include <stack>
#include <queue>
using namespace std;
#define LIMIT (100000+4)
#define DEBUG 0
int main()
{
   if (DEBUG) freopen("stl.01.in", "r", stdin);
   int n; int angles = 0; int count = 0; stack<char> S; queue<char> Q;
   char s[LIMIT][8] = {0};
   scanf("%d", &n); assert(n>0 && n<LIMIT);
   for (int jack = 0; jack < LIMIT; jack++) if (scanf("%s", &s[jack]) == 0) break;
   if (DEBUG) { for (int jack = 0; jack < LIMIT && count != -1; jack++) if (s[jack][0] == 0) break; else printf("%s ", s[jack]); printf("\n"); }
   for (int jack = 0; jack < LIMIT; jack++)
      if (s[jack][0] == 0) break;
      else if (!strcmp("pair", s[jack])) {
         S.push('P'); S.push('B'); angles++;
         Q.push('P'); Q.push('B');
      } else if (!strcmp("int", s[jack])) {
         if (S.empty()) { count++; break; }
         else if (S.top() == 'B') {
            S.push('I'); S.push('C'); count++;
            Q.push('I'); Q.push('C');
         } else if (S.top() == 'C') {
            S.push('I'); S.push('E'); count++; angles--;
            Q.push('I'); Q.push('E');
            if (s[jack+1][0]!=0) Q.push('C');
            while (!S.empty() && S.top()=='E') {
               S.pop(); while (!S.empty() && S.top()!='P') S.pop(); S.pop();
               if (S.empty()) continue;
               if (S.top() == 'B') { S.push('I'); S.push('C'); }
               else if (S.top() == 'C') { S.push('I'); S.push('E'); angles--; Q.push('E'); } else assert(0);
            }
            if (S.empty() && s[jack+1][0]!=0) break;
         }
      }
   if (!S.empty() || count!=n || angles!=0) printf("%s", "Error occurred");
   else while (!Q.empty()) {
      switch (Q.front()) {
         case 'P' : printf("%s", "pair"); break;
         case 'I' : printf("%s", "int"); break;
         case 'B' : printf("%s", "<"); break;
         case 'E' : printf("%s", ">"); break;
         case 'C' : printf("%s", ","); break;
      }
      Q.pop();
   }
   printf("\n");
   if (DEBUG) fclose(stdin);
   return 0;
}


#include "assert.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include <stack>
#include <queue>
using namespace std;
#define LIMIT (100000+4)
#define DEBUG 0
int main()
{
   if (DEBUG) freopen("stl.01.in", "r", stdin);
   int n; int angles = 0; int count = 0; stack<char> S; queue<char> Q;
   char s[LIMIT][8] = {0};
   scanf("%d", &n); assert(n>0 && n<LIMIT);
   for (int jack = 0; jack < LIMIT; jack++) if (scanf("%s", &s[jack]) == 0) break;
   if (DEBUG) { for (int jack = 0; jack < LIMIT && count != -1; jack++) if (s[jack][0] == 0) break; else printf("%s ", s[jack]); printf("\n"); }
   for (int jack = 0; jack < LIMIT; jack++)
      if (s[jack][0] == 0) break;
      else if (!strcmp("pair", s[jack])) {
         S.push('P'); S.push('B'); angles++;
         Q.push('P'); Q.push('B');
      } else if (!strcmp("int", s[jack])) {
         if (S.empty()) { count++; break; }
         else if (S.top() == 'B') {
            S.push('I'); S.push('C'); count++;
            Q.push('I'); Q.push('C');
         } else if (S.top() == 'C') {
            S.push('I'); S.push('E'); count++; angles--;
            Q.push('I'); Q.push('E');
            if (s[jack+1][0]!=0) Q.push('C');
            while (!S.empty() && S.top()=='E') {
               S.pop(); while (!S.empty() && S.top()!='P') S.pop(); S.pop();
               if (S.empty()) continue;
               if (S.top() == 'B') { S.push('I'); S.push('C'); }
               else if (S.top() == 'C') { S.push('I'); S.push('E'); angles--; } else assert(0);
            }
            if (S.empty() && s[jack+1][0]!=0) break;
         }
      }
   if (!S.empty() || count!=n || angles!=0) printf("%s", "Error occurred");
   else while (!Q.empty()) {
      switch (Q.front()) {
         case 'P' : printf("%s", "pair"); break;
         case 'I' : printf("%s", "int"); break;
         case 'B' : printf("%s", "<"); break;
         case 'E' : printf("%s", ">"); break;
         case 'C' : printf("%s", ","); break;
      }
      Q.pop();
   }
   printf("\n");
   if (DEBUG) fclose(stdin);
   return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
string s,ans;
bool flag;
void doit(){
	if (cin>>s && s!=""){
		//cout<<"?"<<s<<"?"<<endl;
		ans+=s;
		if (s=="pair"){

			ans+="<";
			doit();
			ans+=",";
			doit();
			ans+=">";
		}

	}
	else{
		//cout<<":";
		flag=false;
	}
}
int main(){
	int n;
	cin>>n;
	flag=true;
	cin>>s;
	if (s=="pair"){
		ans+=s;
		ans+="<";
		doit();
		ans+=",";
		doit();
		ans+=">";
	}
	else{
		if (n==1){
			cout<<"int";
			return 0;
		}
		cout<<"Error occurred";
	}
//	doit();
	if (flag==false){
		cout<<"Error occurred";
	}
	else{
		cout<<ans;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
string s,ans;
bool flag;
void doit(){
	if (cin>>s && s!=""){
		//cout<<"?"<<s<<"?"<<endl;
		ans+=s;
		if (s=="pair"){

			ans+="<";
			doit();
			ans+=",";
			doit();
			ans+=">";
		}

	}
	else{
		//cout<<":";
		flag=false;
	}
}
int main(){
	int n;
	cin>>n;
	flag=true;
	cin>>s;
	doit();
	if (cin>>s ||flag==false){
		cout<<"Error occurred";
	}
	else{
		cout<<ans;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
string s,ans;
bool flag;
void doit(){
	if (cin>>s && s!=""){
		//cout<<"?"<<s<<"?"<<endl;
		ans+=s;
		if (s=="pair"){

			ans+="<";
			doit();
			ans+=",";
			doit();
			ans+=">";
		}

	}
	else{
		//cout<<":";
		flag=false;
	}
}
int main(){
	int n;
	cin>>n;
	flag=true;
	//cin>>s;
	doit();
	if (cin>>s ||flag==false){
		cout<<"Error occurred";
	}
	else{
		cout<<ans;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
string s,ans;
bool flag;
void doit(){
	if (cin>>s && s!=""){
		cout<<"?"<<s<<"?"<<endl;
		ans+=s;
		if (s=="pair"){
			ans+="<";
			doit();
			ans+=",";
			doit();
			ans+=">";
		}

	}
	else{
		//cout<<":";
		flag=false;
	}
}
int main(){
	int n;
	cin>>n;
	flag=true;
	doit();
	if (flag==false){
		cout<<"Error occurred";
	}
	else{
		cout<<ans;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
string s,ans;
bool flag;
void doit(){
	if (cin>>s && s!=""){
		//cout<<"?"<<s<<"?"<<endl;
		ans+=s;
		if (s=="pair"){

			ans+="<";
			doit();
			ans+=",";
			doit();
			ans+=">";
		}

	}
	else{
		//cout<<":";
		flag=false;
	}
}
int main(){
	int n;
	cin>>n;
	flag=true;
	cin>>s;
	if (s=="pair"){
		ans+=s;
		ans+="<";
		doit();
		ans+=",";
		doit();
		ans+=">";
	}
	else{
		cout<<"Error occurred";
	}
//	doit();
	if (flag==false){
		cout<<"Error occurred";
	}
	else{
		cout<<ans;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
string s,ans;
bool flag;
void doit(){
	if (cin>>s && s!=""){
		//cout<<"?"<<s<<"?"<<endl;
		ans+=s;
		if (s=="pair"){
			ans+="<";
			doit();
			ans+=",";
			doit();
			ans+=">";
		}

	}
	else{
		//cout<<":";
		flag=false;
	}
}
int main(){
	int n;
	cin>>n;
	flag=true;
	doit();
	if (flag==false){
		cout<<"Error occurred";
	}
	else{
		cout<<ans;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
string h[100010];
int f;
pair<string,int> getit(int k){
	string res = "";
	if(k ==f){
		return mp("",-1);
	}
	if(h[k] == "pair"){
		pair<string,int> r= getit(k+1);
		if(r.second == -1)return mp("",-1);
		pair<string,int> t = getit(r.second+1);
		if(t.second == -1)return mp("",-1);
		res += "pair<" + r.first + "," + t.first + ">";
		return mp(res,t.second);
	}
	else{
		return mp("int",k);
	}
}
int main(){
	int n;
	cin>>n;
	int j = 0;
	string q;
	while(cin>>h[j++])
	pair<string,int> ans = getit(0);;
	f = j-1;
	if(ans.second == -1 || ans.second != f-1)cout<<"Error occurred\n";
	else cout<<ans.first<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
string h[100010];
int f;
pair<string,int> getit(int k){
	string res = "";
	if(k ==f){
		return mp("",-1);
	}
	if(h[k] == "pair"){
		pair<string,int> r= getit(k+1);
		if(r.second == -1)return mp("",-1);
		pair<string,int> t = getit(r.second+1);
		if(t.second == -1)return mp("",-1);
		res += "pair<" + r.first + "," + t.first + ">";
		return mp(res,t.second);
	}
	else{
		return mp("int",k);
	}
}
int main(){
	int n;
	cin>>n;
	int j = 0;
	string q;
	while(cin>>h[j++]);
	f = j-1;
	pair<string,int> ans = getit(0);;
	if(ans.second == -1 || ans.second != f-1)cout<<"Error occurred\n";
	else cout<<ans.first<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
string h[100010];
int f;
pair<string,int> getit(int k){
	string res = "";
	if(k ==f){
		return mp("",-1);
	}
	if(h[k] == "pair"){
		pair<string,int> r= getit(k+1);
		if(r.second == -1)return mp("",-1);
		pair<string,int> t = getit(r.second+1);
		if(t.second == -1)return mp("",-1);
		res += "pair<" + r.first + "," + t.first + ">";
		return mp(res,t.second);
	}
	else{
		return mp("int",k);
	}
}
int main(){
	int n;
	cin>>n;
	int j = 0;
	while(cin>>h[j++]);
	pair<string,int> ans = getit(0);;
	if(ans.second == -1 || ans.second != f-1)cout<<"Error occurred\n";
	else cout<<ans.first<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
string h[100010];
int f;
pair<string,int> getit(int k){
	string res = "";
	if(k ==f){
		return mp("",-1);
	}
	if(h[k] == "pair"){
		pair<string,int> r= getit(k+1);
		if(r.second == -1)return mp("",-1);
		pair<string,int> t = getit(r.second+1);
		if(t.second == -1)return mp("",-1);
		res += "pair<" + r.first + "," + t.first + ">";
		return mp(res,t.second);
	}
	else{
		return mp("int",k);
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin>>n;
	int j = 0;
	string q;
	while(cin>>h[j++]);
	pair<string,int> ans = getit(0);;
	f = j-1;
	cout<<f<<'\n';
	for(int i=0;i<f;i++)cout<<h[i]<<" ";
	cout<<'\n';
	if(ans.second == -1 || ans.second != f-1)cout<<"Error occurred\n";
	else cout<<ans.first<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
string h[100010];
int f;
pair<string,int> getit(int k){
	string res = "";
	if(k ==f){
		return mp("",-1);
	}
	if(h[k] == "pair"){
		pair<string,int> r= getit(k+1);
		if(r.second == -1)return mp("",-1);
		pair<string,int> t = getit(r.second+1);
		if(t.second == -1)return mp("",-1);
		res += "pair<" + r.first + "," + t.first + ">";
		return mp(res,t.second);
	}
	else{
		return mp("int",k);
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin>>n;
	int j = 0;
	string q;
	while(cin>>h[j++]);
	f = j-1;
	pair<string,int> ans = getit(0);;
	if(ans.second == -1 || ans.second != f-1)cout<<"Error occurred\n";
	else cout<<ans.first<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
string h[100010];
int f;
pair<string,int> getit(int k){
	string res = "";
	if(k ==f){
		return mp("",-1);
	}
	if(h[k] == "pair"){
		pair<string,int> r= getit(k+1);
		if(r.second == -1)return mp("",-1);
		pair<string,int> t = getit(r.second+1);
		if(t.second == -1)return mp("",-1);
		res += "pair<" + r.first + "," + t.first + ">";
		return mp(res,t.second);
	}
	else{
		return mp("int",k);
	}
}
int main(){
	int n;
	cin>>n;
	int j = 0;
	string q;
	while(cin>>h[j++]);
	pair<string,int> ans = getit(0);;
	f = j-1;
	cout<<f<<'\n';
	for(int i=0;i<f;i++)cout<<h[i]<<" ";
	cout<<'\n';
	if(ans.second == -1 || ans.second != f-1)cout<<"Error occurred\n";
	else cout<<ans.first<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
string h[100010];
int f;
pair<string,int> getit(int k){
	string res = "";
	if(k ==f){
		return mp("",-1);
	}
	if(h[k] == "pair"){
		pair<string,int> r= getit(k+1);
		if(r.second == -1)return mp("",-1);
		pair<string,int> t = getit(r.second+1);
		if(t.second == -1)return mp("",-1);
		res += "pair<" + r.first + "," + t.first + ">";
		return mp(res,t.second);
	}
	else{
		return mp("int",k);
	}
}
int main(){
	int n;
	cin>>n;
	int j = 0;
	string q;
	while(cin>>h[j++]);
	pair<string,int> ans = getit(0);;
	f = j-1;
	cout<<f<<'\n';
	if(ans.second == -1 || ans.second != f-1)cout<<"Error occurred\n";
	else cout<<ans.first<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
string h[100010];
int f;
pair<string,int> getit(int k){
	string res = "";
	if(k ==f){
		return mp("",-1);
	}
	if(h[k] == "pair"){
		pair<string,int> r= getit(k+1);
		if(r.second == -1)return mp("",-1);
		pair<string,int> t = getit(r.second+1);
		if(t.second == -1)return mp("",-1);
		res += "pair<" + r.first + "," + t.first + ">";
		return mp(res,t.second);
	}
	else{
		return mp("int",k);
	}
}
int main(){
	int n;
	cin>>n;
	int j = 0;
	string q;
	while(cin>>q)h[j++]=q;
	pair<string,int> ans = getit(0);;
	if(ans.second == -1 || ans.second != f-1)cout<<"Error occurred\n";
	else cout<<ans.first<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
string h[100010];
int f;
pair<string,int> getit(int k){
	string res = "";
	if(k ==f){
		return mp("",-1);
	}
	if(h[k] == "pair"){
		pair<string,int> r= getit(k+1);
		if(r.second == -1)return mp("",-1);
		pair<string,int> t = getit(r.second+1);
		if(t.second == -1)return mp("",-1);
		res += "pair<" + r.first + "," + t.first + ">";
		return mp(res,t.second);
	}
	else{
		return mp("int",k);
	}
}
int main(){
	int n;
	cin>>n;
	int j = 0;
	string q;
	while(cin>>h[j++]);
	pair<string,int> ans = getit(0);;
	f = j-1;
	if(ans.second == -1 || ans.second != f-1)cout<<"Error occurred\n";
	else cout<<ans.first<<'\n';
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

//kAc
const double pi = acos(-1.0), eps = 1e-9;
const int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1};
const int MO = (int)(1e9 + 7);

#define ALL(x) x.begin(), x.end()
#define fr(x, E) for (__typeof(E.begin()) x = E.begin(); x != E.end(); x++)
#define MP make_pair
#define PB push_back
#define FR first
#define SC second
#define ERR cerr << "ERROR" << endl
#define LL long long
#define LD long double
#define PII pair<int, int>
#define PIII pair<PII, int>
#define PDI pair<double, int>
#define PID pair<int, double>
#define SZ(a) (int)((a).size())
#define VEC vector
#define STR string
#define ISS istringstream
#define OSS ostringstream
#define CLR(a, b) memset(a, b, sizeof(a))
#define gmin(a, b) { if (b < a) a = b; }
#define gmax(a, b) { if (b > a) a = b; }

using namespace std;
OSS oss;
STR s[1000001];
int n, tot;
int doit(int l, int r)
{
	if (l > r) return 0;
	if (l > tot || r > tot) return 0;
	if (s[l] == "int"){
		oss << s[l]; return l;
	}
	else{
		oss << s[l];
		oss << "<";
		int t = doit(l + 1, r);
		if (t == 0) return 0;
		oss << ",";
		t = doit(t + 1, r);
		if (t == 0) return 0;
		oss << ">";
		return t;
	}
}
STR in;
int main()
{
#ifndef ONLINE_JUDGE
	freopen("temp.in", "r", stdin);
#endif
	cin >> n;
	while(cin >> in) s[++tot] = in;
	if (tot != n * 2 - 1) { puts("Error occurred"); return 0; }
	int t = doit(1, tot);
	if (t != tot || t == 0) {puts("Error occurred"); return 0; }
	cout << oss.str() << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
string h[100010];
int f;
string jk ="";
int getit(int k){
	string res = "";
	if(k ==f){
		return -1;
	}
	if(h[k] == "pair"){
		jk +="pair<";
		int r= getit(k+1);
		if(r == -1)return -1;
		jk +=",";
		int t = getit(r+1);
		if(t == -1)return -1;
		jk += ">";
		return t;
	}
	else{
		jk += "int";
		return k;
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin>>n;
	int j = 0;
	string q;
	while(cin>>h[j++]);
	f = j-1;
	int ans = getit(0);;
	if(ans == -1 || ans != f-1)cout<<"Error occurred\n";
	else cout<<jk<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
#define pb push_back
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
string h[100010];
int f;
inline pair<string,int> getit(int k){
	string res = "";
	if(k ==f){
		return mp("",-1);
	}
	if(h[k] == "pair"){
		pair<string,int> r= getit(k+1);
		if(r.second == -1)return mp("",-1);
		pair<string,int> t = getit(r.second+1);
		if(t.second == -1)return mp("",-1);
		res += "pair<" + r.first + "," + t.first + ">";
		return mp(res,t.second);
	}
	else{
		return mp("int",k);
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin>>n;
	int j = 0;
	string q;
	while(cin>>h[j++]);
	f = j-1;
	pair<string,int> ans = getit(0);;
	if(ans.second == -1 || ans.second != f-1)cout<<"Error occurred\n";
	else cout<<ans.first<<'\n';
}


#include "string.h"
#include "strings.h"
#include "iostream"
#include "stdio.h"
#include "math.h"

std::string getPair(int n)
{
    std::string res = "pair<";
    std::string buf;
    if (n>0)
    {
        std::cin>>buf;

        if (buf.compare("pair") == 0)
        {
            res += getPair(n);
        }
        else
        {
            n--;
            res += buf;
        }
        res += ",";
        if (n>0)
        {
            std::cin>>buf;
            n--;
            if (buf.compare("pair") == 0)
            {
                res += getPair(n);
            }
            else
            {
                n--;
                res += buf;
            }
            res += ">";
            return res;
        }
        else
        {
            return "Error occurred";
        }
    }
    else
    {
        return "Error occurred";
    }
}

int main()
{
    int n;
    std::string buf;
    std::string result;

    std::cin>>n;
    if (n > 0)
    {
        std::cin>>buf;
        if (buf.compare("pair") == 0)
        {
           if(n>=2)
               result += (getPair(n));
           else
               result += "Error occurred";
        }
        else
        {
            n--;
        }
    }
    if (std::string::npos != result.find("Error occurred"))
        result = "Error occurred";

    std::cout<<result;

    return 0;
}


#include "string.h"
#include "strings.h"
#include "iostream"
#include "stdio.h"
#include "math.h"

std::string getPair(int &n)
{
    std::string res = "pair<";
    std::string buf;
    if (n>0)
    {
        std::cin>>buf;

        if (buf.compare("pair") == 0)
        {
            std::string p = getPair(n);
            if (p.compare("Error occurred") == 0)
                return "Error occurred";
            res += p;
        }
        else
        {
            n--;
            res += buf;
        }
        res += ",";
        if (n>0)
        {
            std::cin>>buf;
            if (buf.compare("pair") == 0)
            {
                std::string p = getPair(n);
                if (p.compare("Error occurred") == 0)
                    return "Error occurred";
                res += p;
            }
            else
            {
                n--;
                res += buf;
            }
            res += ">";
            return res;
        }
        else
        {
            return "Error occurred";
        }
    }
    else
    {
        return "Error occurred";
    }
}

int main()
{
    int n;
    std::string buf;
    std::string result;

    std::cin>>n;
    if (n > 0)
    {
        std::cin>>buf;
        if (buf.compare("pair") == 0)
        {
           if(n>=2)
               result += (getPair(n));
           else
               result += "Error occurred";
        }
        else
        {
            n--;
        }
    }
    //if (std::string::npos != result.find("Error occurred") && n> 0)
     //   result = "Error occurred";

    std::cout<<result;

    return 0;
}


#include "string.h"
#include "strings.h"
#include "iostream"
#include "stdio.h"
#include "math.h"

std::string getPair(int &n)
{
    std::string res = "pair<";
    std::string buf;
    if (n>0)
    {
        std::cin>>buf;

        if (buf.compare("pair") == 0)
        {
            std::string p = getPair(n);
            if (p.compare("Error occurred") == 0)
                return "Error occurred";
            res += p;
        }
        else
        {
            n--;
            res += buf;
        }
        res += ",";
        if (n>0)
        {
            std::cin>>buf;
            if (buf.compare("pair") == 0)
            {
                std::string p = getPair(n);
                if (p.compare("Error occurred") == 0)
                    return "Error occurred";
                res += p;
            }
            else
            {
                n--;
                res += buf;
            }
            res += ">";
            return res;
        }
        else
        {
            return "Error occurred";
        }
    }
    else
    {
        return "Error occurred";
    }
}

int main()
{
    int n;
    std::string buf;
    std::string result;

    std::cin>>n;
    if (n > 0)
    {
        std::cin>>buf;
        if (buf.compare("pair") == 0)
        {
           if(n>=2)
               result += (getPair(n));
           else
               result += "Error occurred";
        }
        else
        {
            result += buf;
            n--;
        }
    }
    if (n> 0)
        result = "Error occurred";

    std::cout<<result;

    return 0;
}


#include "string.h"
#include "strings.h"
#include "iostream"
#include "stdio.h"
#include "math.h"

std::string getPair(int &n)
{
    std::string res = "pair<";
    std::string buf;
    //std::cout<<n;
    if (n>0)
    {
        std::cin>>buf;

        if (buf.compare("pair") == 0)
        {
            res += getPair(n);
        }
        else
        {
            n--;
            res += buf;
        }
        res += ",";
        if (n>0)
        {
            std::cin>>buf;
            n--;
            if (buf.compare("pair") == 0)
            {
                res += getPair(n);
            }
            else
            {
                n--;
                res += buf;
            }
            res += ">";
            return res;
        }
        else
        {
            return "Error occurred";
        }
    }
    else
    {
        return "Error occurred";
    }
}

int main()
{
    int n;
    std::string buf;
    std::string result;

    std::cin>>n;
    if (n > 0)
    {
        std::cin>>buf;
        if (buf.compare("pair") == 0)
        {
           if(n>=2)
               result += (getPair(n));
           else
               result += "Error occurred";
        }
        else
        {
            n--;
        }
    }
    if (std::string::npos != result.find("Error occurred"))
        result = "Error occurred";

    std::cout<<result;

    return 0;
}


#include "string.h"
#include "strings.h"
#include "iostream"
#include "stdio.h"
#include "math.h"

std::string getPair(int &n)
{
    std::string res = "pair<";
    std::string buf;
    if (n>=2)
    {
        std::cin>>buf;

        if (buf[0] == 'p')
        {
            std::string p = getPair(n);
            if (p[0] == 'E')
                return "Error occurred";
            res += p;
        }
        else
        {
            n--;
            res += buf;
        }
        res += ",";
        std::cin>>buf;
        if (buf[0] == 'p')
        {
            std::string p = getPair(n);
            if (p[0] == 'E')
                return "Error occurred";
            res += p;
        }
        else
        {
            n--;
            res += buf;
        }
        res += ">";
        return res;
    }
    else
    {
        return "Error occurred";
    }
}

int main()
{
    int n;
    std::string buf;
    std::string result;

    std::cin>>n;
    std::cin>>buf;
    if (buf[0] == 'p')
        result += (getPair(n));
    else
        result += buf;

    std::cout<<result;

    return 0;
}


#include "string.h"
#include "strings.h"
#include "iostream"
#include "stdio.h"
#include "math.h"

std::string getPair(int &n)
{
    std::string res = "pair<";
    std::string buf;
    if (n>0)
    {
        std::cin>>buf;

        if (buf.compare("pair") == 0)
        {
             std::string p = getPair(n);
                if (std::string::npos != p.find("Error occurred"))
                    return "Error occurred";
                res += p;
        }
        else
        {
            n--;
            res += buf;
        }
        res += ",";
        if (n>0)
        {
            std::cin>>buf;
            if (buf.compare("pair") == 0)
            {
                std::string p = getPair(n);
                if (std::string::npos != p.find("Error occurred"))
                    return "Error occurred";
                res += p;
            }
            else
            {
                n--;
                res += buf;
            }
            res += ">";
            return res;
        }
        else
        {
            return "Error occurred";
        }
    }
    else
    {
        return "Error occurred";
    }
}

int main()
{
    int n;
    std::string buf;
    std::string result;

    std::cin>>n;
    if (n > 0)
    {
        std::cin>>buf;
        if (buf.compare("pair") == 0)
        {
           if(n>=2)
               result += (getPair(n));
           else
               result += "Error occurred";
        }
        else
        {
            n--;
        }
    }
    //if (std::string::npos != result.find("Error occurred") && n> 0)
     //   result = "Error occurred";

    std::cout<<result;

    return 0;
}


#include<iostream>
#include<string>

using namespace std;

string ans;

int exp(){
  string s;
  cin>>s;
  if(s=="int"){
    ans+=s;
    return 0;
  }
  if(s=="pair"){
    ans+="pair<";
    if(exp())return 1;
    ans+=',';
    if(exp())return 1;
    ans+='>';
    return 0;
  }
  return 1;
}

int main(){
  int n;
  cin>>n;
  string s;
  if(exp())cout<<"Error occurred"<<endl;
  else{
    if(cin>>s)cout<<"Error occurred"<<endl;
    else cout<<ans<<endl;
  }
  return 0;
}


#include "string.h"
#include "strings.h"
#include "iostream"
#include "stdio.h"
#include "math.h"

std::string getPair(int &n)
{
    std::string res = "pair<";
    std::string buf;
    if (n>0)
    {
        std::cin>>buf;

        if (buf[0] == 'p')
        {
            std::string p = getPair(n);
            if (p[0] == 'E')
                return "Error occurred";
            res += p;
        }
        else
        {
            n--;
            res += buf;
        }
        res += ",";
        if (n>0)
        {
            std::cin>>buf;
            if (buf[0] == 'p')
            {
                std::string p = getPair(n);
                if (p[0] == 'E')
                    return "Error occurred";
                res += p;
            }
            else
            {
                n--;
                res += buf;
            }
            res += ">";
            return res;
        }
        else
        {
            return "Error occurred";
        }
    }
    else
    {
        return "Error occurred";
    }
}

int main()
{
    int n;
    std::string buf;
    std::string result;

    std::cin>>n;
    if (n > 0)
    {
        std::cin>>buf;
        if (buf[0] == 'p')
        {
           if(n>=2)
               result += (getPair(n));
           else
               result += "Error occurred";
        }
        else
        {
            result += buf;
            n--;
        }
    }
    if (n> 0)
        result = "Error occurred";

    std::cout<<result;

    return 0;
}


#include "string.h"
#include "strings.h"
#include "iostream"
#include "stdio.h"
#include "math.h"

std::string getPair(int &n)
{
    std::string res = "pair<";
    std::string buf;
    if (n>0)
    {
        std::cin>>buf;

        if (buf[0] == "p")
        {
            std::string p = getPair(n);
            if (p[0] == "E")
                return "Error occurred";
            res += p;
        }
        else
        {
            n--;
            res += buf;
        }
        res += ",";
        if (n>0)
        {
            std::cin>>buf;
            if (buf[0] == "p")
            {
                std::string p = getPair(n);
                if (p[0] == "E")
                    return "Error occurred";
                res += p;
            }
            else
            {
                n--;
                res += buf;
            }
            res += ">";
            return res;
        }
        else
        {
            return "Error occurred";
        }
    }
    else
    {
        return "Error occurred";
    }
}

int main()
{
    int n;
    std::string buf;
    std::string result;

    std::cin>>n;
    if (n > 0)
    {
        std::cin>>buf;
        if (buf[0] == "p")
        {
           if(n>=2)
               result += (getPair(n));
           else
               result += "Error occurred";
        }
        else
        {
            result += buf;
            n--;
        }
    }
    if (n> 0)
        result = "Error occurred";

    std::cout<<result;

    return 0;
}