#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;

#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;

#define BIG_NUM 2000000000

using namespace std;

enum TYPE{
	NONE,
	FIRST,
	SECOND,
	LEFT,
	RIGHT
};

struct Node{
	Node(){
		parent = first_index = address = left_child=right_child = -1;
		route_index = 0;
		check = false;
	}
	void init(){
		parent = first_index = address = left_child=right_child = -1;
		route_index = 0;
		check = false;
	}

	void addRoute(char dist){
		route[route_index++] = dist;
		route[route_index] = '\0';
	}

	int left_child,right_child,address,route_index,first_index,parent;
	char route[150];
	bool check;
};

bool strCmp(char* base, char* comp){
	int length1 = 0,length2 = 0;
	for(int i=0;base[i] != '\0'; i++)length1++;
	for(int i=0;comp[i] != '\0'; i++)length2++;
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

void strcpy(char* to,char* str){
	for(int i=0;str[i] != '\0';i++){
		to[i] = str[i];
		to[i+1] = '\0';
	}
}

Node first[120],second[120],third[120];
int first_index = 0,second_index = 0;
char buf[1010];

TYPE toWrite;

void recursive(int left,int right,int parent_address,TYPE left_or_right){
	int calcDepth,nextRight,nextLeft,nextParentAddress = -1;
	if(parent_address != -1){
		calcDepth = 0;
		for(int i = left; i <= right; i++){

			if(calcDepth==1 && buf[i] == ','){
				if(toWrite == FIRST){
					first[first_index].address = first_index; //??????????????¢?????¬???
					first[first_index].parent = parent_address;
					strcpy(first[first_index].route,first[parent_address].route);
					first[first_index].route_index = first[parent_address].route_index;
					if(left_or_right == LEFT){
						first[first_index].addRoute('L');
						first[parent_address].left_child = first_index;
					}else{
						first[first_index].addRoute('R');
						first[parent_address].right_child = first_index;
					}
					nextParentAddress = first_index;
					first_index++;
				}else{
					second[second_index].address = second_index;
					second[second_index].parent = parent_address;
					strcpy(second[second_index].route,second[parent_address].route);
					second[second_index].route_index = second[parent_address].route_index;
					if(left_or_right == LEFT){
						second[second_index].addRoute('L');
						second[parent_address].left_child = second_index;
					}else{
						second[second_index].addRoute('R');
						second[parent_address].right_child = second_index;
					}
					nextParentAddress = second_index;
					second_index++;
				}
				nextRight = i-1;
				nextLeft = i+1;
				break;
			}else{
				if(buf[i] == '(')calcDepth++;
				else if(buf[i] == ')')calcDepth--;
			}
		}

		if(nextParentAddress != -1){
			if(left+1 < nextRight){
				recursive(left+1,nextRight,nextParentAddress,LEFT);
			}
			if(nextLeft < right-1){
				recursive(nextLeft,right-1,nextParentAddress,RIGHT);
			}
		}

	}else{
		calcDepth = 0;
		for(int i = 0; i <= right; i++){
			if(buf[i] == '(')calcDepth++;
			else if(buf[i] == ')')calcDepth--;
			else{
				if(calcDepth==1 && buf[i] == ','){

					if(toWrite == FIRST){
						first[0].address = 0;
						first[0].addRoute('X');
						first_index++;
					}else{
						second[0].address = 0;
						second[0].addRoute('X');
						second_index++;
					}
					nextRight = i-1;
					nextLeft = i+1;
					break;
				}
			}
		}

		if(left+1 < nextRight){
			recursive(left+1,nextRight,0,LEFT);
		}
		if(nextLeft < right-1){
			recursive(nextLeft,right-1,0,RIGHT);
		}
	}
}

void outPut(int address){

	if(third[address].left_child != -1 || third[address].right_child != -1){
		printf("(");
		if(third[address].left_child != -1){
			outPut(third[address].left_child);
		}else{
			//Do nothing
		}
		printf(",");
		if(third[address].right_child != -1){
			outPut(third[address].right_child);
		}else{
			//Do nothing
		}
		printf(")");
	}else{
		printf("(,)");
	}
}


int main(){

	char order[2];

	while(scanf("%s",order)!= EOF){

		first_index = 0,second_index = 0;

		for(int i = 0; i < 120; i++){
			first[i].init();
			second[i].init();
			third[i].init();
		}

		scanf("%s",buf);
		int length;
		for(length = 0; buf[length] != '\0';length++);
		toWrite = FIRST;
		recursive(0,length-1,-1,NONE);

		scanf("%s",buf);
		for(length = 0; buf[length] != '\0';length++);
		toWrite = SECOND;
		recursive(0,length-1,-1,NONE);

		if(order[0] == 'i'){

			int indexTable[1000];

			indexTable[0] = 0;

			int third_index = 1;

			for(int i = 1; i < first_index; i++){
				for(int k = 1; k < second_index; k++){
					if(strCmp(first[i].route,second[k].route)){
						third[third_index].first_index = i;
						strcpy(third[third_index].route,first[i].route);
						third[third_index].route_index = first[i].route_index;
						indexTable[i] = third_index;
						third_index++;
					}
				}
			}

			int p_tmp;

			for(int i = 1; i < third_index; i++){
				p_tmp = indexTable[first[third[i].first_index].parent];
				if(third[i].route[third[i].route_index-1] == 'R'){
					third[p_tmp].right_child = i;
				}else{
					third[p_tmp].left_child = i;
				}
			}

		}else{

			int third_index;

			third[0].left_child = first[0].left_child;
			third[0].right_child = first[0].right_child;
			strcpy(third[0].route,first[0].route);
			third[0].route_index = first[0].route_index;

			for(third_index = 1; third_index < first_index;third_index++){
				strcpy(third[third_index].route,first[third_index].route);
				third[third_index].route_index = first[third_index].route_index;
				third[third_index].left_child = first[third_index].left_child;
				third[third_index].right_child = first[third_index].right_child;
				third[third_index].parent = first[third_index].parent;
			}

			bool restFLG,loopFLG;
			char work[150];

			while(true){
				restFLG = true;

				for(int i = 1; i < second_index; i++){
					if(second[i].check == true)continue;
					loopFLG = false;
					for(int k = 1; k < third_index;k++){
						if(strCmp(second[i].route,third[k].route)){
							loopFLG = true;
							break;
						}
					}
					if(!loopFLG){
						restFLG = false;

						strcpy(work,second[i].route);
						work[second[i].route_index-1] = '\0';

						for(int k = 1; k < third_index; k++){
							if(strCmp(work,third[k].route)){
								if(second[i].route[second[i].route_index-1] == 'R'){
									third[k].right_child = third_index;
									third[third_index].parent = k;
									strcpy(third[third_index].route,second[i].route);
									third[third_index].route_index = second[i].route_index;
									third_index++;
								}else{
									third[k].left_child = third_index;
									third[third_index].parent = k;
									strcpy(third[third_index].route,second[i].route);
									third[third_index].route_index = second[i].route_index;
									third_index++;
								}
								restFLG = true;
							}
						}

						if(!restFLG)break;
					}else{
						second[i].check = true;
					}
				}
				if(restFLG)break;
			}
		}

		outPut(0);
		printf("\n");
	}

	return 0;
}