/*
 * $File: p1001.cpp
 * $Author: Wayne <tletletle@163.com>
 *
 * Task:
 *   Given two binary trees, we need to
 *   calculate the intersection or the
 *   union of them.
 *
 * Solution:
 *   String parsing. Graph theory.
 *   Just Build the trees and implement.
*/

#include <cstdio>
#include <cstring>

const int kMaxVtx = 100;
const int kMaxBuf = 512;
char buf[kMaxBuf];

struct TreeNode {
	TreeNode *son[2];
	TreeNode() {
		son[0] = son[1] = NULL;
	}
};

struct Tree {
	TreeNode *root;
	Tree() {
		root = NULL;
	}
} a, b, c;

TreeNode *BuildTree(int l, int r) {
	if (l > r) return NULL;
	TreeNode *res = new TreeNode;
	if (l + 1 == r - 1) return res;
	int mid, sum = 0;
	for (int i = l + 1; i <= r - 1; ++ i)
		if (buf[i] == '(') ++ sum;
		else if (buf[i] == ')') -- sum;
		else if (buf[i] == ',' && sum == 0) mid = i;
	res->son[0] = BuildTree(l + 1, mid - 1);
	res->son[1] = BuildTree(mid + 1, r - 1);
	return res;
}

void ReadTree(Tree &des) {
	scanf("%s", buf);
	des.root = BuildTree(0, strlen(buf) - 1);
}

TreeNode *DfsIntersection(TreeNode *u, TreeNode *v) {
	if (!u || !v) return NULL;
	TreeNode *res = new TreeNode;
	res->son[0] = DfsIntersection(u->son[0], v->son[0]);
	res->son[1] = DfsIntersection(u->son[1], v->son[1]);
	return res;
}

TreeNode *DfsUnion(TreeNode *u, TreeNode *v) {
	if (!u && !v) return NULL;
	TreeNode *res = new TreeNode;
	res->son[0] = DfsUnion(u ? u->son[0] : NULL, v ? v->son[0] : NULL);
	res->son[1] = DfsUnion(u ? u->son[1] : NULL, v ? v->son[1] : NULL);
	return res;
}

Tree GetIntersection(const Tree &a, const Tree &b) {
	Tree res;
	res.root = DfsIntersection(a.root, b.root);
	return res;
}

Tree GetUnion(const Tree &a, const Tree &b) {
	Tree res;
	res.root = DfsUnion(a.root, b.root);
	return res;
}

void OutputTree(TreeNode *cur) {
	if (!cur) return;
	putchar('(');
	OutputTree(cur->son[0]);
	putchar(',');
	OutputTree(cur->son[1]);
	putchar(')');
}

int main() {
	static char cmd[2];
	while (scanf("%s", cmd) != EOF) {
		ReadTree(a);
		ReadTree(b);
		if (cmd[0] == 'i') c = GetIntersection(a, b);
		else c = GetUnion(a, b);
		OutputTree(c.root);
		puts("");
	}
	return 0;
}