semeru/Code-Code-CloneDetection-POJ104 huggingface dataset:
int main()
{
    int left=0,right=0;
    int times=0,i;
    char s[101];
    char c[101];
    char fakes[101];
    
    for (times=0; times==0;i=0,left=0,right=0) {
        scanf("%s",s);
        if (strcmp(fakes, s)==0&&strlen(fakes)==strlen(s)) {
            break;
        }
        for (i=0; s[i]!='\0'; i++) {
            if (s[i]=='(') {
                left++;
            }
            if (s[i]==')'&&left>0) {
                left--;
            }
            else
            if (s[i]==')'&&left==0) {
                c[i]='?';
            }
        }
        for (; i>=0; i--) {
            if (s[i]==')') {
                right++;
            }
            if (s[i]=='('&&right>0) {
                right--;
            }
            else
            if (s[i]=='('&&right==0) {
                c[i]='$';
            }
        }
        puts(s);
        printf("\n");
        for (i=0; s[i]!='\0'; i++) {
            if (c[i]=='?'||c[i]=='$') {
                printf("%c",c[i]);
                c[i]=0;
            }
            else
                printf(" ");
        }
        printf("\n");
        strcpy(fakes,s);
    }
    return 0;
}
int main()
{
	typedef struct _Parenthesis
	{
		char ch;
		int idx;
	} Parenthesis;
	Parenthesis paren[100];
	char str[101];
	while(gets(str))
	{
		int i,top=0;
		puts(str);
		for(i=0; str[i]; i++)
		{
			if(str[i] == '(')
			{
				paren[top].ch = '(';
				paren[top].idx = i;
				top++;
			}
			else if(str[i] == ')')
			{
				if(top == 0)
					str[i] = '?';
				else
				{
					str[i] = ' ';
					str[paren[--top].idx] = ' ';
				}
			}
			else
				str[i] = ' ';
		}
		for(i=0; i<top; i++)
			str[paren[i].idx] = '$';
		puts(str);
	}
	return 0;
}
int main()
{
	int n,k,i,j,flag=0;
	char s[M]={""};
	scanf("%s",s);
	while((s[0]>='a'&&s[0]<='z')||(s[0]>='A'&&s[0]<='Z')||s[0]=='('||s[0]==')')
	{
		
			printf("%s\n",s);
			n=strlen(s);
			for(i=n-1;i>=0;i--)
			{
				if(s[i]=='(')
				{
					for(j=i+1;j<n;j++)
					{
						if(s[j]==')')
						{
							s[i]='a';
					        s[j]='a';
					        break;
						}
					}
				}
			}
			for(i=0;i<n;i++)
			{
				if(s[i]=='('||s[i]==')')
				{
					flag=1;
			        break;
				}
			}
			if(flag==0)
			{
				for(i=0;i<n;i++)
			    printf("%c",' ');
				printf("\n");
			}
			else
			{
				for(i=0;i<n;i++)
				{
					if(s[i]=='(')
						printf("%c",'$');
					else if(s[i]==')')
						printf("%c",'?');
					else
						printf("%c",' ');
				}
				printf("\n");
			}
			s[0]='\0';
		scanf("%s",s);
	}
	return 0;
}
int brackets(void){
	int chart[2][100],c,count=0,pos,state = 0,tmp,first_left,previous_left,end_flag = 0;
	/*state:0 for search left(start,you have to move right)
	        1 for have left and search right(you have to move right)
			2 for search previous left(you have to move left)
			*/
	for(;;count++){
		c = getchar();
		if(c == EOF)return 1;
		if(c == '\n'){break;}
		chart[0][count] = c;
		if(c == 40){chart[1][count] = 36;} //$
		else if(c == 41){chart[1][count] = 63;} //?
		else{chart[1][count] = 32;} //
	}
	for(pos=0;end_flag == 0;){
		switch(state){
		case 0:
			if(chart[1][pos] == 36){
				state = 1; //switch to state 1
				first_left = pos; //note the current first left bracket
				previous_left = pos; //note the previous left bracket
				if(pos == count){end_flag = 1;}
				else{pos += 1;} //move right
			}
			else{
				if(pos == count){end_flag = 1;} //end of loop
				else{pos += 1;}//move right
			}
			break;
		case 1:
			if(chart[1][pos] == 63){
				chart[1][pos] = 32; //match
				chart[1][previous_left] = 32;
				if(previous_left != first_left){
					state = 2; //switch to state 2
					pos = previous_left; //jump to previous left
				}
				else{
					if(pos == count){end_flag = 1;}
					else{
					    state = 0;
					    pos += 1;
					}
				}
			}
			else if(chart[1][pos] == 36){
				previous_left = pos; // renew previous left bracket
				pos += 1; //move right
			}
			else{
				if(pos == count){end_flag = 1;}
				else{pos += 1;} //find a space,move right
			}
			break;
		case 2:
			if(chart[1][pos] == 36){
				previous_left = pos;
				state = 1;
			}
			else{pos -= 1;}
			break;
		}
		if(pos > count){pos = count;}//force position
	}
	for(tmp=0;tmp < count;tmp++){
		putchar(chart[0][tmp]);
		if(tmp == count - 1){putchar('\n');}
	}
	for(tmp=0;tmp < count;tmp++){
		putchar(chart[1][tmp]);
		if(tmp == count - 1){putchar('\n');}
	}
	return 0;
}
int main(){
	while(1){
        if(brackets() == 1)break;
	}
    return 0;
}
char A[100],a[100];
int i,j=0,m;
int k;
int main()
{
	void pipei1(char x);
	void pipei2(char x);
	int n;
	while(gets(a))
	{
		i=0;
		m=0;
		k=strlen(a);
		if(k==0)
			break;
		for(n=0;n<k;n++)
		{
			A[n]=' ';
			cout<<a[n];
		}
		pipei1(a[j]);
		pipei2(a[j]);
		cout<<endl;
		for(n=0;n<k;n++)
		{
			cout<<A[n];
		}
		cout<<endl;
	}
	 
	return 0;
}
void pipei1(char x)
{
	if(j<k)
	{
		if(i<0)
			i=0;
		if(x=='(')
	    {
		    i++;
	    }
	    else if(x==')')
	    {
		    if(i==0)
		    {
			    A[j]='?';
		    }
			i--;
	    }
	    j++;
	    pipei1(a[j]);
	}
}
void pipei2(char x)
{
	if(j>=0)
	{
		if(m<0)
			m=0;
		if(x==')')
		{
			m++;
		}
		else if(x=='(')
		{
			if(m==0)
			{
				A[j]='$';
			}
			m--;
		}
		j--;
		pipei2(a[j]);
	}
}
int main()
{
    char chuan[101],shuchu[100];
    int count1,count2,length,i,j;
    while(cin>>chuan)
    {
                cout<<chuan<<endl;
                length=strlen(chuan);//????? 
                strcpy(shuchu,chuan);//???????????????? 
                count1=0;
                count2=0;
                for(i=0;i<length;i++)
                {
                                     if(chuan[i]=='(')count1++;
                                     if(chuan[i]==')')
                                     {
                                                      count2++;
                                                      if(count2>count1)
                                                      {
                                                                       shuchu[i]='?'; 
                                                                       count2--;
                                                      }//?????????????????????????????? 
                                     } 
                                     
                }//?????????
                
                count1=0;
                count2=0;
                for(i=length-1;i>=0;i--)
                {
                                     if(chuan[i]==')')count2++;
                                     if(chuan[i]=='(')
                                     {
                                                      count1++;
                                                      if(count1>count2)
                                                      {
                                                                       shuchu[i]='$';
                                                                       count1--;
                                                      }//?????????????????????????????? 
                                     } 
                                     
                }//????????? 
                shuchu[length]='\0';
                for(i=0;i<length;i++)
                {
                                     if(shuchu[i]!='?' && shuchu[i]!='$')shuchu[i]=' ';
                }//????????? 
                cout<<shuchu<<endl; 
                memset(shuchu,0,sizeof(shuchu));
                memset(chuan,0,sizeof(chuan));
    }      
return 0;    
}
int main()
{ 
	int n;
	int i;
	int t;
	scanf("%d",&n);
	scanf("\n");
	char c[101]={0};
	char d[101]={0};
    int j=0,k=0;
	for(i=0;i<n;i++)
	{  
        for(j=0;j<101;j++)
		{
			c[j]=0;
			d[j]=0;
		}
	
	    scanf("%s",c);
		t=strlen(c);
		for(j=0;j<t;j++)
		{
			d[j]=c[j];
		}
		for(j=0;j<t;j++)
		{
			if(c[j]!='('&&c[j]!=')') c[j]=' ';
			if(c[j]==')')
			{
				for(k=j-1;k>=0;k--)
				{
					if(c[k]=='(')
					{
						c[k]=' ';
						c[j]=' ';
						break;
					}
				}
			}
		}
		for(j=0;j<t;j++)
		{
			if(c[j]=='(')
			{
				c[j]='$';
			}
			else if(c[j]==')')
			{
				c[j]='?';
			}
		}
	
		printf("%s",d);
		printf("\n");
		printf("%s",c);
		
		printf("\n");
	}
		return 0;	
}
int findl(int i,char a[102],int n);
int find(int i,char a[102]);
int main()
{
	char a[102];
	memset (a,'\n',102);
	while(cin >>a)
	{
		int n=strlen(a);
		cout <<a <<endl;
		for (int i=0;i<n;i++)
		{
			int x=0;
			if (a[i]=='('&&findl(i,a,n)>n) cout <<"$";        //????
			else if (a[i]==')'&&find(i,a)<0) cout <<'?';
			else cout <<" ";
		}
		cout <<endl;
	}
}
int findl(int i,char a[102],int n)               //????????
{
	int k=i+1;
	while (k<n)
	{
		if (a[k]=='(') k=findl(k,a,n);           //????????????
		else if (a[k]==')') return k;
		k++;
	}
	return n+1;
}
int find(int i,char a[102])                       //???????
{
	int k=i-1;
	while (k>=0)
	{
		if (a[k]==')') k=find (k,a);
		else if (a[k]=='(') return k;
		k--;
	}
	return -1;
}
/*
 * 3704:??????
 *
 *  Created on: 2012-12-1
 *      Author: zhuyongfu
 */


char s[101];//?????????????
int l;//??????
int f(int k){//????
	int i,j;
	for(i = k;i >= 0;i--)//??????
		if(s[i] == '(')
		{
			for(j = i + 1;j < l;j++)
				if(s[j] == ')')//????????????
				{
						s[i] = 'a';//?????????
						s[j] = 'a';
						k = i;
						return f(k);//??
				}
		}
	for(i = 0;i < l;i++)//??
	{
		if(s[i] == '(')
			cout << '$';
		else if(s[i] == ')')
			cout << '?';
		else
			cout << ' ';
	}
	cout << endl;
	return 0;
}

int main(){
	while(cin.getline(s,101,'\n')){//??
		l = strlen(s);
		cout << s << endl;
		f(l - 1);//????
		memset(s,'\0',sizeof(s));//??
	}
	return 0;
}
int main()
{
    char a[105];
    while(gets(a))
    {
        int n=strlen(a);int i=0,j,b[105];
        for(i=0;i<n;i++)
        {
            if(a[i]=='('){b[i]=1;}
            else if(a[i]==')'){b[i]=-1;}
            else b[i]=0;
        }//first step:change char a[] to int b[].
        for(i=0;i<n;i++)
        {
            if(b[i]==-1)
            {
                for(j=i;j>=0;j--)
                {
                    if(b[j]==1)
                    {
                        b[i]=0;b[j]=0;break;
                    }
                }

            }
        }
        puts(a);printf("\n");
        for(i=0;i<n;i++)
        {
            if(b[i]==0)
                printf(" ");
            else if(b[i]==1)
                printf("$");
            else printf("?");
        }

        printf("\n");




    }
    return 0;
}
int main()
{
	char str[110];
	int i=0,j=0;
	while(cin.getline(str,110))
	{
		for(i=0;str[i]!='\0';i++) //???????
		{
			cout<<str[i];
		}
		cout<<endl;
		for(i=0;str[i]!='\0';++i) //????????????????????????????????????
		{
			if(str[i]==')')
			{
				for(j=i-1;j>=0;--j)
				{
					if(str[j]=='(')
					{
						str[i]=str[j]=' ';
						break;
					}
				}
			}
		}
		for(i=0;i<strlen(str);++i) //??????????????
		{
			if(str[i]=='(')
				cout<<"$";
			else if(str[i]==')')
				cout<<"?";
			else
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}
int show(char a[],int t)
{int i,k,l,j=1;
static int b[101];
puts(a);
for(i=0;i<=t-1;i++)
switch(a[i])
{case '(':a[i]=' ';
          b[j]=i+1;
		  j++;
		  break;
case ')':a[i]=' ';
          b[j]=-(i+1);
		  j++;
		  break;
default: a[i]=' ';
}
j--;
do
{
l=0;
for(k=1;k<=j-1;k++)
if(b[k]>0)
{for(i=k+1;i<=j;i++)
if(b[i]!=0)
break;
if(b[k]*b[i]<0)
{b[k]=0;b[i]=0;l++;}}
}
while(l>0);
for(i=1;i<=j;i++)
{if(b[i]<0)
a[-b[i]-1]='?';
else if(b[i]>0)
a[b[i]-1]='$';
}
puts(a);
return 0;
}
int main()
{int i,n;
scanf("%d\n",&n);
for(i=1;i<=n;i++)
{char a[101];int t;
gets(a);
t=strlen(a);
show(a,t);}
return 0;}
int main()
{
	int n;
	scanf("%d",&n);
	char c[10000];
	int i;
	for(i=0;i<n;i++)
	{
		scanf("%s",c);
		printf("%s\n",c);
	    int j,x;
		x=strlen(c);
		for(j=0;j<x;j++)
		{
			if(c[j]!='('&&c[j]!=')')
			{
				c[j]=' ';
			}
		}
		for(j=0;j<x;j++)
		{
			if(c[j]==')')
			{
				int m;
				for(m=j;m>=0;m--)
				{
					if(c[m]=='(')
					{
						c[m]=' ';
						c[j]=' ';
						break;
					}
				}
			}
		}
		for(j=0;j<x;j++)
		{
			if(c[j]=='(')
			{
				c[j]='$';
			}
			else if(c[j]==')')
			{
				c[j]='?';
			}
		}
		printf("%s\n",c);
	}
	return 0;
}
//?????????????????????????????????????
//???????????????;break;???????
//??????????????????

int main(int argc, char* argv[])
{
	char s[100];
	int i,j;
	
	while(scanf("%s",s)!=EOF)
	{
		puts(s);
		int a[100]={0};
		for(i=0;s[i]!='\0';i++)
			if(s[i]=='(')
				a[i]=-1;
			else if(s[i]==')')
				a[i]=1;
			
			for(i=0;s[i]!='\0';i++)
				if(a[i]==1)
					for(j=i-1;j>=0;j--)
						if(a[j]==-1)
						{
							a[i]=a[j]=0;
							break;
						}
						
			for(i=0;s[i]!='\0';i++)
				if(a[i]==1) printf("?");
				else if(a[i]==-1) printf("$");
				else if(a[i]==0) printf(" ");
			printf("\n");
	}

	return 0;
}
void find(char a[],int n,int m)
{
	int i;
	if(a[m]!=')'&&m<n)
	{find(a,n,m+1);}//??????????
	if(a[m]==')'&&m<n)
	{
		for(i=m;i>=0;i--)
		{
			if(a[i]=='(')
			{a[i]=' ';a[m]=' ';break;}//????????
		}
		find(a,n,m+1);//????
	}
}
int main()
{
	int n;
	char a[200];
	while(cin>>a)//??
	{
	cout<<a<<endl;//??????
	n=strlen(a);
	find(a,n,0);//????????
	for(int k=0;k<n;k++)
	{
		if(a[k]=='(')
		{a[k]='$';}//???????
		else 
			{if(a[k]==')')//???????
		{a[k]='?';}
		else 
			{if(a[k]!='('&&a[k]!=')')//?????????
		{a[k]=' ';}
		}
		}
	}
	cout<<a<<endl;//???????
	}
	return 0;
}
//****************************************
//*?????????                   **
//*?????? 1100012763              **
//*???2011.11.15                     **
//****************************************
int main() 
{
	char array[101] , ans[101];
	int len , i , left , right;
	
	while (cin.getline(array , 101))
	{
		len = strlen(array);
		left = 0; 
		right = 0;
		for (i = 0; i < len; i++)
		{
			ans[i] = ' ';
            if (array[i] == '(')
			{
				left++;
			}
			if (array[i] == ')')
			{
				right++;
				if (right > left)
				{
					left = 0;
					right = 0;
					ans[i] = '?';
				}
			}
		}
		left = 0;
		right = 0;
		for (i = len - 1; i >=0; i--)
		{
			if (array[i] == '(')
			{
				left++;
				if (left > right)
				{
					left = 0;
					right = 0;
					ans[i] = '$';
				}
			}
			if (array[i] == ')')
			{
				right++;
			}
		}
		for (i = 0; i < len; i++)
			cout << array[i];
		cout << endl;
		for (i = 0; i < len; i++)
			cout << ans[i];
		cout << endl;		
	}

	return 0;
}
int main(){
	char string[101];//??????
	int i,j,lenth,n,t;
	while(cin.getline(string,101)){//??????????????
		n=0;
		lenth=strlen(string);//????
		cout<<string<<endl;//????????
		for(i=0;i<lenth;i++){//??????????????????????
			if(string[i]=='(')  string[i]='$';
			else if(string[i]==')')  string[i]='?';
			else{
				string[i]=' ';
				n++;
			}
		}
		n=(lenth-n)/2;//?????????????
        for(i=0;i<n;i++){//??
        	for(j=0;j<lenth;j++){
        		for(t=j+1;t<lenth;t++){
        			if(string[j]=='$'&&string[t]=='$'){
        			    j=t;
        			    continue;
        			}
        			if(string[j]=='$'&&string[t]=='?'){
        				string[j]=string[t]=' ';
        				j=t-1;
        			}
        		}
        	}
        }
        for(i=lenth-1;i>=0;i--){//??????
        	if(string[i]!=' ') break;
        }
        string[i+1]='\0';
        lenth=strlen(string);
        int m=0;
        for(i=0;i<lenth;i++){//??????
        	if(string[i]!=' ') break;
        	m++;
        }
        for(i=m;i<lenth;i++)
        	cout<<string[i];
        cout<<endl;
	}
	return 0;
}
struct bracket
{
	char data[max];
	int top;
};

int main()
{
	char str[max], flag[max];
	int i, j, len,n;
	struct bracket *leftbracket, *rightbracket;
	scanf("%d\n",&n);
	for(j=0;j<n;j++)
	{
		scanf("%s",str);
		//????
		leftbracket = (struct bracket *)malloc(sizeof(struct bracket));
		rightbracket = (struct bracket *)malloc(sizeof(struct bracket));
		leftbracket->top = rightbracket->top = 0;

		len = strlen(str);

		//?????????????
		for(i = 0; i < len; i ++)
		{
			switch(str[i])
			{
				case '(' :
					leftbracket->data[leftbracket->top ++] = str[i];
					flag[i] = ' ';
					break;
				case ')' :
					if(leftbracket->top > 0)
					{
						flag[i] = ' ';
						leftbracket->top -= 1;
					}else
					{
						flag[i] = '?';
					}
					break;
				default :
					flag[i] = ' ';
					break;
			}
		}

		//?????????????
		for(i = len - 1; i >= 0; i --)
		{
			switch(str[i])
			{
				case ')' :
					rightbracket->data[rightbracket->top ++] = str[i];
					break;
				case '(' :
					if(rightbracket->top > 0)
					{
						rightbracket->top -= 1;
					}else
					{
						flag[i] = '$';
					}
					break;
				default :
					break;
			}
		}
		flag[len]='\0';
		//????
		printf("%s\n%s\n", str, flag);

		//?????
		memset(str, '\0', sizeof(str));
		memset(flag, '\0', sizeof(flag));
	}
	return 0;
}
int getlength(char* a)//???????
{
	int i=0;
	while(a[i]!='\0')
		i++;
	return i;
}
void chuli(char ch[],int a[],int n)//???????????int???
{
	for(int i=0;i<n;i++)
	{
		switch(ch[i])
		{
		case '(':a[i]=1;break;
		case ')':a[i]=-1;break;
		default :a[i]=0;break;
		}
	}
}
void pipei(int a[],int n)//????????
{
	for(int k=0;k<n;k++)
	{
		if(a[k]==1)
		{
			int s=0;
			for(int i=0;i<k;i++)
				s+=a[i];
			for(int i=k;i<n;i++)
			{
				if(a[i]==-1)
				{
					int t=0;
					for(int j=0;j<i;j++)
						t+=a[j];
					if(s==t-1)//???? 
					{a[k]=0;a[i]=0;break;}//??????0
				}
			}
		}
	}
}
int main()
{
	while(cin.peek()!=EOF)
	{
		char ch[110];
		cin.getline(ch,110,'\n');
		int a[110];
		int n=getlength(ch);
		chuli(ch,a,n);
		pipei(a,n);
		cout<<ch<<endl;
		for(int i=0;i<n;i++)//??
		{
			switch(a[i])
			{
			case 1:cout<<'$';break;
			case -1:cout<<'?';break;
			case 0:cout<<' ';break;
			}		
                 }
		cout<<endl;
	}
	return 0;
}
int main()
{
	char l,r,s,sen[200],rem[200];
	int i=1,j=1,k=1,nsen,b=1;
	l='(';r=')';

	while(b==1)
	{

		while((s = cin.get())!='\n')
		{sen[i]=s;rem[i]=s;i++;
		}nsen=i-1;

		for(j=1;j<nsen;j++)//??????
			for(i=1;i<nsen;i++)
				if(sen[i]==l && sen[i+j]==r)
				{sen[i]=' ';sen[i+j]=' ';}//?????????????

				for(i=1;i<=nsen;i++)
					if(sen[i]==l)sen[i]='$';
					else if(sen[i]==r)sen[i]='?';//?????????
					else sen[i]=' ';//?????????


					for(i=1;i<=nsen;i++)
						cout<<rem[i];
					cout<<endl;
					for(i=1;i<=nsen;i++)
						cout<<sen[i];
					cout<<endl;

					nsen=0;i=1;j=1;k=1;
	}


	return 0;
}
void kuohao(char c[])
{
	int i,j,n;
	for(n=0;c[n]!='\0';n++);//???????
	for(i=n-1;i>=0;i--)
	{
		if(c[i]=='(')
		{
			for(j=i+1;c[j]!='\0';j++)
				if(c[j]==')')
				{c[i]=c[j]=' ';break;}
			if(c[j]=='\0')//??????????????????????$
			{c[i]='$';}
			kuohao(c);//????
		}
	}
}

int main()
{
	char c[110]={'\0'};
	int i;
	while(cin.getline(c,101))
	{cout<<c<<endl;
	for(i=0;c[i]!='\0';i++)
		if(c[i]!='('&&c[i]!=')')
			c[i]=' ';//????????????
	kuohao(c);
	for(i=0;c[i]!='\0';i++)
		if(c[i]==')')
			c[i]='?';//???????????
	cout<<c<<endl;}
	return 0;
}
char a[1000];
int n, c1 = 0;
int match(int c)
{
	for(int i = c; i < n; i++)
	{
		if(a[i] != '(' && a[i] != ')' && a[i] != '$')
			a[i] = ' ';
		if(a[i] == '(')
		{
			c1 ++;
			if(match(i + 1) != 0)
			{
				a[i] = ' ';
				a[match(i + 1)] = ' ';
				c1 --;
			}
			else
				a[i] = '$';
		}
		if(a[i] == ')')
		{
			if(c1 == 0)
			{
				a[i] = '?';
				if(i < n)
				{
					match(i + 1);
					return 0;
				}
			}
			return i;
		}
	}
	return 0;
}
int main()                                  //??? 
{
	while(cin>>a)
	{
		c1 = 0;
		n = strlen(a);
		cout<<a<<endl;
		match(0);
		cout<<a<<endl<<endl;
	}
    return 0; 
}                                          //?????
int main()
{
	char ch1[101],ch2[101];
	int i=0,j=0,k=0,len=0;
	while(cin.getline(ch1,101))
	{
		len=strlen(ch1);
		int num=0;
		for(i=0;i<len;i++)
		{
			ch2[i]=' ';
			if(ch1[i]=='(')
			{
				ch2[i]='$';
				num++;
			}
			if(ch1[i]==')')
			{
				ch2[i]='?';
				num--;
				if(num==0)
				{
					ch2[i]=' ';
					for(j=0;j<i;j++)
					{
						if(ch1[j]=='(')
						{
							for(k=j;k<=i;k++)
							{
								ch2[k]=' ';
								break;break;
							}
						}
					}
				}
				if(num>0)
				{
					ch2[i]=' ';
					for(j=i;j>=0;j--)
					{
						if(ch2[j]=='$')
						{
							ch2[j]=' ';
							break;
						}
					}
				}
				if(num<0)
					num=0;
				
			}
		}
		ch2[len]='\0';
		cout<<ch1<<endl;
		cout<<ch2<<endl;
	}
	return 0;
}
int main()
{
	char a[101];  //?????a[101]????????????
	int s[101],len,k=0,i;  //????s[101]???????len????????k????????;i??????
	while(cin>>a)
	{
		for(len=0;a[len]!='\0';len++);  //????????
		cout<<a<<endl;
		for(i=0;i<len;i++)  //????????????
		{
			if(a[i]=='(')  //?a[i]?'('??a[i]???
				s[k++]=i;
			if(a[i]==')'&&k>0)  //?a[i]?')'???????????'('??????
			{
				a[s[--k]]=' ';
				a[i]=' ';
			}
			if(a[i]!='('&&a[i]!=')')  //?a[i]??????????
				a[i]=' ';
			if(a[i]==')'&&k==0)
				a[i]='?';
		}
		for(i=k-1;i>=0;i--)
			a[s[i]]='$';
		cout<<a<<endl;
		memset(a,0,sizeof(a));  //??????
		k=0;
	}
	return 0;
}
int main()
{
    char s[101] = {0};
    while ( (scanf("%s", s) != EOF) )
    {
      int i,j;
	  int you=0;
	  int l1=strlen(s);
	  char a[101];
	  strcpy(a,s);
	  for(i=l1-1;i>=0;i--)
	  {
	    if(s[i]=='(') 
		{
			
			you=0;
		for(j=i+1;j<l1;j++)
		{

		  if(s[j]==')')
		  {
			s[i]=' ';
			s[j]=' ';
			you=you+1;
			break;
		  }
		}
		if(you==0) s[i]='$';
		}
	  }
	  puts(a);
	  
     for(i=0;i<l1;i++)
	 {
	   if(s[i]=='?'||s[i]=='$') printf("%c",s[i]);
       else if(s[i]==')') printf("?");
	   else printf(" ");
	 }
	 printf("\n");
	}
    return 0;
}
int len;
void f(char a[])
{
	int i,j;
	len=strlen(a); 
	for(i=len-1;i>=0;i--)
	{
		if(a[i]=='(')
		{
			for(j=i+1;j<len;j++)
			{
				if(a[j]==')')
				{
					a[i]=a[j]=' ';
					break;	
				}	
			}
			if(a[j]=='\0')
			{
				a[i]='$';	
			}
				f(a);	
		}	
	}

}
int main()
{
	char c[110]={'\0'};
	int i;
	while(cin.getline(c,101))
	{cout<<c<<endl;
	for(i=0;c[i]!='\0';i++)
		if(c[i]!='('&&c[i]!=')')
			c[i]=' ';//????????????
f(c);
	for(i=0;c[i]!='\0';i++)
		if(c[i]==')')
			c[i]='?';//???????????
	cout<<c<<endl;}
	return 0;
}
int main()
{
	char string[1000];
	int i=0,j=0,length=0,t1=0,t2=0;
	int flag=0,left=0,right=0;
	while ( gets( string ) )
	{
		cout<<string<<endl;
		length=strlen(string);
		flag=1,left=0,right=0;                        //???
		t1=0,t2=0;                                    //???
		while ( flag==1 )
		{
			for ( i=0;i<length;i++ )
			{
				if ( string[i]=='(' )
				{
					left=i;
					t1=1;
				}
				//????????,?????????,???????????
				else if ( t1==1 && string[i]==')' )
				{
					right=i;
					t2=1;
					break;
				}
				//??,?????????????????,??????break????????
				else
					continue;
			}
			
			if ( t1==1 && t2==1 )
				//??????????????????????????????'0'
			{
				string[left]='0';
				string[right]='0';
				t1=0;
				t2=0;
				flag=1;
			}
			else          //?????????????????????????????????????????
				flag=0;
			//cout<<string<<endl;
		}

		for ( i=0;i<length;i++ )                 //??????????????
		{
			if ( string[i]=='(' )
				string[i]='$';
			else if ( string[i]==')' )
				string[i]='?';
			else
				string[i]=' ';
		}

		for ( i=0;i<length;i++ )
		{
			cout<<string[i];
		}
		cout<<endl;
	}
	return 0;
}
void zhengli(char a[])
{
	void panduan(char a[]);
	int i,k,j;
	k=strlen(a);
	for(j=0;j<50;j++)
	{
		for(i=0;i<k;i++)
		{
			if(a[i]=='$')
				a[i]='(';
			else if(a[i]=='?')
				a[i]=')';
		}
		panduan(a);
	}
}
void panduan(char a[])
{
	int i,k,flag=0,j;
	char b[110];
	k=strlen(a);
	b[k]='\0';
	for(i=0;i<k;i++)
	{
		if(a[i]!='('&&a[i]!=')')
			b[i]=' ';
		else if(a[i]=='(')
		{
			if(flag==1)
			{
				b[j]='$';
				j=i;
				b[j]='$';
			}
			else
			{
				flag=1;
				j=i;
				b[j]='$';
			}
		}
		else
		{
			if(flag==1)
			{
				b[j]=' ';
				flag=0;
				b[i]=' ';
			}
			else
			{
				b[i]='?';
			}
		}
	}
	strcpy(a,b);
}
void main()
{
	int i,k;
	char a[110];
	scanf("%d",&k);
	for(i=0;i<k;i++)
	{
		scanf("%s",a);
		printf("%s\n",a);
		panduan(a);
		zhengli(a);
		printf("%s\n",a);
	}
}