#include <bitset>
#include <iostream>
#include <string>

using namespace std;

struct node {
  node *l;
  node *r;
  node *p;
};

node *parse(string str) {

  node *base = new node({});
  node *p = base;

  bool flag = true;
  for (auto &c : str) {
    switch (c) {
    case '(': {
      node *p_new = new node({nullptr, nullptr, p});
      if (flag)
        p = p->l = p_new;
      else
        p = p->r = p_new;
      flag = true;
      break;
    }
    case ',': {
      flag = false;
      break;
    }
    case ')': {
      p = p->p;
      break;
    }
    };
  }
  return base->l;
}

void print_tree_intersection(node *tree1, node *tree2, bool endl_flag=true) {
  if ( (tree1 == nullptr) || (tree2 == nullptr) )
    return;

  cout << '(';
  print_tree_intersection(tree1->l, tree2->l, false);
  cout << ',';
  print_tree_intersection(tree1->r, tree2->r, false);
  cout << ')';

  if (endl_flag)
    cout << endl;
}

void print_tree_union(node *tree1, node *tree2, bool endl_flag=true) {
  if ( (tree1 != nullptr) && (tree2 != nullptr) ) {
    cout << '(';
    print_tree_union(tree1->l, tree2->l, false);
    cout << ',';
    print_tree_union(tree1->r, tree2->r, false);
    cout << ')';
  } else if ( (tree1 != nullptr) || (tree2 != nullptr) ) {
    node *p = (tree1) ? tree1 : tree2;
    cout << '(';
    print_tree_union(p->l, nullptr, false);
    cout << ',';
    print_tree_union(p->r, nullptr, false);
    cout << ')';
  }

  if (endl_flag)
    cout << endl;
}

int main() {

  while (true) {
    char iu;
    string str1, str2;
    cin >> iu >> str1 >> str2;

    if (cin.fail())
      break;

    node *tree1 = parse(str1);
    node *tree2 = parse(str2);

    if (iu == 'i')
      print_tree_intersection(tree1, tree2);
    else
      print_tree_union(tree1, tree2);
  }
}
