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


struct Tree{
	Tree *l,*r;
	Tree():l(0),r(0){}
	~Tree(){delete l;delete r;}
};

Tree* to_t(string s){
	Tree *t=new Tree();
	if(s=="")return 0;
	if(s=="(,)")return t;
	int lev=0;
	for(int i=0;i<s.size();i++){
		if(s[i]=='(')lev++;
		else if(s[i]==')')lev--;
		else if(lev==1&&s[i]==','){
			t->l=to_t(s.substr(1,i-1));
			t->r=to_t(s.substr(i+1,s.size()-(i+1)-1));
			break;
		}
	}
	return t;
}
string intersection(Tree *s,Tree *t){
	string res;
	if(!s||!t)return res;
	res+='(';
	res+=intersection(s->l,t->l);
	res+=',';
	res+=intersection(s->r,t->r);
	res+=')';
	return res;
}
string _union(Tree *s,Tree *t){
	string res;
	if(!s&&!t)return res;
	res+='(';
	res+=_union(s?s->l:0,t?t->l:0);
	res+=',';
	res+=_union(s?s->r:0,t?t->r:0);
	res+=')';
	return res;
}


int main(){
	char op;
	string a,b;
	Tree * s,*t;
	while(cin>>op>>a>>b){
		s=to_t(a);
		t=to_t(b);
		if(op=='i')cout<<intersection(s,t)<<endl;
		else cout<<_union(s,t)<<endl;
		delete s;
		delete t;
	}
	return 0;
}