#include <iostream>
using namespace std;

struct Node {
  Node *left, *right;
  
  Node() : left(NULL), right(NULL) {}
  
  ~Node() {
    delete left;
    delete right;
  }
};

const char *p;

Node *parse(){ 
  Node *ret = new Node;
  
  if(*++p == '(') {
    ret->left = parse();
  }
  if(*++p == '(') {
    ret->right = parse();
  }
  ++p;
  
  return ret;
}

Node *child(Node *node, string op) {
  return node ? ( op == "l" ? node->left : node->right ) : NULL;
}

Node *traverse(string op, Node *node1, Node *node2) {
  Node *ret = NULL;
  
  if(op == "i") {
    if(node1 && node2) {
      ret = new Node;
    }
  } else {
    if(node1 || node2) {
      ret = new Node;
    }
  }
  
  if(ret != NULL) {
    ret->left = traverse(op, child(node1, "l"), child(node2, "l"));
    ret->right = traverse(op, child(node1, "r"), child(node2, "r"));
  }
  
  return ret;
}

string tostr(Node *node) {
  if(node == NULL) return "";
  return '('  + tostr(node->left) + ',' + tostr(node->right) + ')';
}

int main() {
  
  string op, str1, str2;
  
  while(cin >> op >> str1 >> str2) {
    Node *root1, *root2, *res;
    p = str1.c_str();
    root1 = parse();
    p = str2.c_str();
    root2 = parse();

    res = traverse(op, root1, root2);
    string out;
    out = tostr(res);
    cout << out << endl;
    
    delete root1;
    delete root2;
    delete res;
  }
  
  return 0;
}