#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 150

enum Type{
	LEFT_STRONG,
	RIGHT_STRONG,
	EVEN,
};

struct Node{
	int left_child,right_child,height,partial_num;
	double value;
	string partial_tree;
	set<string> SET;
};

string line;
int num_nodes;
Node nodes[NUM];

Type compare_value(int left_id,int right_id){

	if(fabs(nodes[left_id].value-nodes[right_id].value) > EPS){
		if(nodes[left_id].value < nodes[right_id].value){
			return LEFT_STRONG;
		}else{
			return RIGHT_STRONG;
		}
	}
	if(nodes[left_id].left_child == -1 && nodes[right_id].left_child == -1){
		return EVEN;
	}
	int L_1 = nodes[left_id].left_child;
	int R_1 = nodes[left_id].right_child;
	int L_2 = nodes[right_id].left_child;
	int R_2 = nodes[right_id].right_child;

	int first_1,second_1,first_2,second_2;

	if(compare_value(L_1,R_1) != RIGHT_STRONG){

		first_1 = L_1;
		second_1 = R_1;

	}else{

		first_1 = R_1;
		second_1 = L_1;

	}

	if(compare_value(L_2,R_2) != RIGHT_STRONG){

		first_2 = L_2;
		second_2 = R_2;

	}else{

		first_2 = R_2;
		second_2 = L_2;
	}

	Type ret = compare_value(first_1,first_2);
	if(ret != EVEN){
		return ret;
	}

	return compare_value(second_1,second_2);
}

int makeTree(int left,int right){

	int node_id = num_nodes++;

	if(left == right){

		nodes[node_id].height = 1;
		nodes[node_id].partial_num = 1;
		nodes[node_id].value = 0;
		nodes[node_id].partial_tree = "x";
		nodes[node_id].SET.insert("x");

		return node_id;
	}

	int depth = 0,mid = -1;

	for(int i = left; i <= right; i++){
		if(line[i] == '('){

			depth++;

		}else if(line[i] == ')'){
			depth--;
		}

		if(line[i] == ' ' && depth == 1){
			mid = i;
			break;
		}
	}

	int left_child = makeTree(left+1,mid-1);
	int right_child = makeTree(mid+1,right-1);

	if((nodes[left_child].height < nodes[right_child].height) ||
		(nodes[left_child].height == nodes[right_child].height && nodes[left_child].partial_num < nodes[right_child].partial_num) ||
		(nodes[left_child].height == nodes[right_child].height && nodes[left_child].partial_num == nodes[right_child].partial_num &&
				compare_value(left_child,right_child) == RIGHT_STRONG)){
		swap(left_child,right_child);
	}

	nodes[node_id].left_child = left_child;
	nodes[node_id].right_child = right_child;

 	nodes[node_id].height = nodes[left_child].height+1;
 	nodes[node_id].partial_num = nodes[left_child].partial_num+nodes[right_child].partial_num+1; //自分を含めた部分木の数

 	set<string>::iterator at,at2;
 	for(at = nodes[left_child].SET.begin(); at != nodes[left_child].SET.end(); at++){
 		nodes[node_id].SET.insert(*at);
 	}
 	for(at = nodes[right_child].SET.begin(); at != nodes[right_child].SET.end(); at++){
		nodes[node_id].SET.insert(*at);
	}

 	int num_common = 0;
	for(at = nodes[left_child].SET.begin(); at != nodes[left_child].SET.end(); at++){
		for(at2 = nodes[right_child].SET.begin(); at2 != nodes[right_child].SET.end(); at2++){
			if(*at == *at2){
				num_common++;
			}
		}
	}
	nodes[node_id].value = (double)num_common/(double)nodes[node_id].SET.size();

	nodes[node_id].partial_tree = "(" + nodes[left_child].partial_tree+ " " + nodes[right_child].partial_tree + ")";
 	nodes[node_id].SET.insert(nodes[node_id].partial_tree);

	return node_id;
}

void changeTree(int node_id,bool is_left_branch){

	if(nodes[node_id].left_child == -1){
		return;
	}

	Type type = compare_value(nodes[node_id].left_child,nodes[node_id].right_child);

	if((type == LEFT_STRONG && is_left_branch == false) || (type == RIGHT_STRONG && is_left_branch == true)){

		swap(nodes[node_id].left_child,nodes[node_id].right_child);
	}

	changeTree(nodes[node_id].left_child,true);
	changeTree(nodes[node_id].right_child,false);
}

void printTree(int node_id){

	if(nodes[node_id].left_child == -1){
		printf("x");
		return;
	}

	printf("(");
	printTree(nodes[node_id].left_child);
	printf(" ");
	printTree(nodes[node_id].right_child);
	printf(")");
}

void func(){

	for(int i = 0; i < NUM; i++){

		nodes[i].left_child = -1;
		nodes[i].right_child = -1;

		nodes[i].partial_tree.clear();
		nodes[i].SET.clear();
	}

	num_nodes = 0;

	int length = line.length();

	makeTree(0,length-1);

	changeTree(0,true);

	printTree(0);
	printf("\n");
}

int main(){

	while(true){
		getline(cin,line);
		if(line[0] == '0')break;

		func();
	}

	return 0;
}



Measured memory_bytes: 3232