#include <iostream>
#include <stdio.h>
#include <stack>
#include <queue>
using namespace std;

typedef struct Node{
    Node *lChild;
    Node *rChild;
} Node;

Node *createTree(string brackets);
void printTree(Node *root);
void getBrackets(Node *Tree, queue<char> &brackets);
Node *intersectionTree(Node *Tree1, Node *Tree2);
Node *unionTree(Node *Tree1, Node *Tree2);

int main(int argc, const char * argv[]) {
    string kind, bracTree1, bracTree2;
    Node *Tree1, *Tree2, *newTree;
    queue< queue<char> > outputs;
    
    while( cin>>kind>>bracTree1>>bracTree2 ){
        Tree1 = createTree(bracTree1);
        Tree2 = createTree(bracTree2);
        
        if(kind == "i"){
            newTree = intersectionTree(Tree1, Tree2);
        }
        else{
            newTree = unionTree(Tree1, Tree2);
        }
        
        queue<char> newBrac;
        getBrackets(newTree, newBrac);
        outputs.push(newBrac);
    }
    
    queue<char> output;
    while (!outputs.empty()) {
        output = outputs.front();
        while (!output.empty()) {
            printf("%c",output.front());
            output.pop();
        }
        printf("\n");
        outputs.pop();
    }
    
    return 0;
}

Node *createTree(string brackets){
    bool meetComma;
    Node *left, *right, *root = NULL;
    stack<char> Stack;
    stack<Node *> nodeStack;
    
    for(int i = 0; i <= brackets.length(); i++){
        if( brackets[i] == ')'){
            char c;
            left = NULL, right =NULL, root = NULL;
            meetComma = false;
            
            while (true) {
                c = Stack.top();
                Stack.pop();
                if(c == '('){
                    break;
                }
                else if( c == ','){
                    meetComma = true;
                }
                else if( c == 'N' ){
                    if(meetComma){
                        left = nodeStack.top();
                    }
                    else{
                        right = nodeStack.top();
                    }
                    nodeStack.pop();
                }
                else{
                    cout<<"error"<<endl;
                }
            }
            
            root = new Node;
            root->lChild = left;
            root->rChild = right;
            nodeStack.push(root);
            Stack.push('N');
        }
        else{
            Stack.push(brackets[i]);
        }
    }
    return root;
}

void printTree(Node *root){
    if ( root == NULL ) {
        return;
    }
    
    printf("(");
    printTree(root->lChild);
    printf(",");
    printTree(root->rChild);
    printf(")");
}

Node *intersectionTree(Node *Tree1, Node *Tree2 ){
    Node *iTree = NULL;
    if(Tree1 != NULL && Tree2 != NULL){
        iTree = new Node;
        iTree->lChild = intersectionTree(Tree1->lChild, Tree2->lChild);
        iTree->rChild = intersectionTree(Tree1->rChild, Tree2->rChild);
    }
    return iTree;
}

Node *unionTree(Node *Tree1, Node *Tree2){
    Node *uTree ;
    if(Tree1 == NULL && Tree2 == NULL){
        uTree = NULL;
    }
    else if(Tree1 == NULL){
        uTree = Tree2;
    }
    else if(Tree2 == NULL){
        uTree = Tree1;
    }
    else{
        uTree = new Node;
        uTree->lChild = unionTree(Tree1->lChild, Tree2->lChild);
        uTree->rChild = unionTree(Tree1->rChild, Tree2->rChild);
    }
    return uTree;
}

void getBrackets(Node *Tree, queue<char> &brackets){
    if(Tree == NULL){
        return;
    }
    
    brackets.push('(');
    getBrackets(Tree->lChild, brackets);
    brackets.push(',');
    getBrackets(Tree->rChild, brackets);
    brackets.push(')');
}

Measured memory_bytes: 3096