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

struct Node {
  Node *ch[2];
  Node() { ch[0] = ch[1] = NULL; }
};

void build(Node *node, const string &s, int &i) {
  if( s[i] == '(' ) {
    i++; // (
    node->ch[0] = new Node();
    build(node->ch[0], s, i);
    i++; // ,
    node->ch[1] = new Node();
    build(node->ch[1], s, i);
    i++; // )
  }
}

void print(Node* node) {
  if( node == NULL || (node->ch[0] == NULL && node->ch[1] == NULL) ) return;
  cout << "(";
  print(node->ch[0]);
  cout << ",";
  print(node->ch[1]);
  cout << ")";
}

void erase(Node *node) {
  for(int i = 0; i < 2; i++) if( node->ch[i] ) erase(node->ch[i]);
  delete node;
}

void Intersection(Node *res, Node *a, Node *b) {
  for(int i = 0; i < 2; i++)
    if( a && a->ch[i] && b && b->ch[i] ) {
      res->ch[i] = new Node();
      Intersection(res->ch[i], a->ch[i], b->ch[i]);
    }
}

void Union(Node *res, Node *a, Node *b) {
  for(int i = 0; i < 2; i++)
    if( (a && a->ch[i]) || (b && b->ch[i]) ) {
      res->ch[i] = new Node();
      Union(res->ch[i], ( a ? a->ch[i] : NULL ), (b ? b->ch[i] : NULL ));
    }
}


int main() {
  for(string cmd, A, B; cin >> cmd >> A >> B;) {
    Node *roota = new Node();
    Node *rootb = new Node();
    Node *rootc = new Node();

    int i = 0;
    build(roota, A, i);

    i = 0;
    build(rootb, B, i);

    if( cmd[0] == 'i' ) Intersection(rootc, roota, rootb);
    else Union(rootc, roota, rootb);

    print(rootc); cout << endl;

    erase(roota);
    erase(rootb);
    erase(rootc);
  }
}