// ??????.cpp : Defines the entry point for the console application.
//

int main(int argc, char* argv[])
{
	char zfc[120],cha,i,j,len;
	while(scanf("%s",zfc)!=EOF){
		cha=0;
		len=strlen(zfc);
		printf("%s\n",zfc);
		for(i=0;i<len;i++){
			if(zfc[i]!='('&&zfc[i]!=')'){
				zfc[i]=' ';
			}
			if(zfc[i]==')'){
				for(j=i-1;j>=0;j--){
					if(zfc[j]=='('){
						zfc[j]=' ';
						zfc[i]=' ';
						break;
					}
				}
				if(j<0){
					zfc[i]='?';
				}
			}
		}
		for(i=0;i<len;i++){
			if(zfc[i]=='('){
				zfc[i]='$';
			}
		}
		puts(zfc);
	}
	return 0;
}


int main()
{
	char line[101] ={0}, label[101]={0};
	int pos[101] = {0}, left, right, l, i, j, n;
    cin >> n;
	for (j = 0; j < n; j++)
	{
		cin >> line;
		left = 0;
		right = 0;
		l = strlen(line);
		for (i = 0; i < 101; i++)
			label[i] = ' ';
		for (i = 0; line[i] != '\0'; i++)
		{
			if (line[i] == '(')
			{
				left++;
				pos[left] = i;
			}
			if (line[i] == ')')
			{
				if (left <= right)
					label[i] = '?';
				else
					left--;
			}
		}
		for (i = 1; i <= left; i++)
			label[pos[i]] = '$';
		label[l] = '\0';
		cout << line << endl;
		cout << label << endl;
	}
	return 0;
}


int main()
{
	char string[200];
	int leftpos,mark;
	while(scanf("%s",string)==1)
	{
		printf("%s\n",string);
		leftpos=-1;
		mark=0;
		while(mark!=strlen(string))
		{
			if(string[mark]=='(') leftpos=mark;
			else if(string[mark]==')'&&leftpos!=-1)
			{
				string[mark]=' ';
				string[leftpos]=' ';
				mark=-1;
				leftpos=-1;
			}
			mark++;
		}
		for(mark=0;mark<strlen(string);mark++)
		{
			if(string[mark]=='(') string[mark]='$';
			else if(string[mark]==')') string[mark]='?';
			else string[mark]=' ';
		}
		printf("%s\n",string);
		memset(string,0,200);
	}
	return 0;
}


int main(){
	int ghus(char a[]);
	void noghus(char a[]);
	int n,i,t,j;
	char b[500];
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%s",b);
		t=ghus(b);
		printf("%s\n",b);
		for(j=1;j<=t;j++){
			noghus(b);
		}
		for(j=0;j<strlen(b);j++){
			if(b[j]=='(') printf("$");
			else if(b[j]==')') printf("?");
			else printf(" ");
		}
		printf("\n");
	}

}

int min(int a,int b){
	if(a<b) return a;
	else return b;
}

int ghus(char a[]){
	int min(int a,int b);
	int m=0,n=0,i;
	for(i=0;i<strlen(a);i++){
		if(a[i]=='(') m++;
		if(a[i]==')') n++;
	}
	return min(m,n);
}

void noghus(char a[]){
	int i,j;
	for(i=0;i<strlen(a)-1;i++){
			if(a[i]=='('){
				for(j=i+1;j<strlen(a);j++){
					if(a[j]==')'||a[j]=='(') break;
				}
				if(a[j]==')'){
					a[i]='o';
					a[j]='o';
				}
			}
	}
}


void Replace(char s[],int x)
{
	for(int i=x+1;i<strlen(s);i++)
	{
	if(s[i]==')'){s[x]=s[i]=' ';return;}
	if(s[i]=='(')Replace(s,i);
	}
	s[x]='$';
}
void Find(char s[])
{
	for(int i=0;i<strlen(s);i++)
	{
if(s[i]=='$'||s[i]=='?')continue;
		if(s[i]=='(')Replace(s,i);
		else if(s[i]==')')s[i]='?';
		else {
			s[i]=' ';
		}
	}
	return;
}

int main()
{
	char live[101];//string live;
	while(cin>>live)
	{
		cout<<live<<endl;
		Find(live);
		cout<<live<<endl;
	}
	return 0;
}


void braket_match(char *s);
int main()
{
    int n,i;
    char tmp[MAXLEN];
    scanf("%d ",&n);
    for(i=0;i<n;i++)
    {
        gets(tmp);
        braket_match(tmp);
    }
    return 0;
}
void braket_match(char *s)
{
    printf("%s\n",s);
    int j,left[MAXLEN]={-1,-1},left_num=0,left0=1,right[MAXLEN]={-1,-1},right_num=0,right0=1,LEN=strlen(s);
    for(j=0;j<LEN;j++)
    {
        if(s[j]=='(')
            left[++left_num]=j;
        else if(s[j]==')'&&left_num==0)
            right[++right_num]=j;
        else if(s[j]==')'&&left_num>0)
            left[left_num--]=-1;
    }
    for(j=0;j<LEN;j++)
    {
        if(j==left[left0])
        {
            printf("$");
            left0++;
        }
        else if(j==right[right0])
        {
            printf("?");
            right0++;
        }
        else
            printf(" ");
    }
    printf("\n");
}


int main()
{
	int n=0;
	scanf("%d",&n);

	int i=0;
	char string[101];
	char string_copy[101];
	char string_check[101];
	int len=0;
	int index=0;
	int start=0;
	int turn=0;
	char y='n';

	for(i=0;i<=n;i++)
	{
		len=0;
		gets(string);
		while(string[len])
			len++;

		strcpy(string_copy,string);

		for(index=0;index<len;index++)
		{
			if(string_copy[index]!='(' && string_copy[index]!=')')
				string_copy[index]=' ';
			if(string[index]=='(' || string[index]==')')
				y='y';
		}
		if(y=='n')
			continue;

		for(turn=0;turn<=len/2;turn++)
		{
			y='n';
			start=0;
			for(index=0;index<len;index++)
			{
				if(string_copy[index]=='(')
					start=index;
				if(string_copy[index]==')' && string_copy[start]=='(')
				{
					string_copy[index]=' ';
					string_copy[start]=' ';
				}
			}
		}

		strcpy(string_check,string_copy);
		for(index=0;index<len;index++)
		{
			if(string_copy[index]=='(')
				string_copy[index]='$';
			if(string_copy[index]==')')
				string_copy[index]='?';
		}
		if(strcmp(string_copy,string_check)!=0)
		{
			printf("%s\n%s\n",string,string_copy);
		}
		else
		{
			printf("%s\n",string);
		}
	}

	return 0;
}


int zuok(char c[],int j, int len)
{
	int i,zuo=1,you=0;
	for(i=j+1;i<len;i++)
	{
		if(c[i]=='(')
		{
			zuo++;
		}
		else if(c[i]==')')
		{
			you++;
		}
		if(zuo==you)
		{
			return 1;
		}
	}
	return 0;
}
int youk(char c[],int j,int len)
{
	int i,you=1,zuo=0;
	for(i=j-1;i>=0;i--)
	{
		if(c[i]=='(')
		{
			zuo++;
		}
		else if(c[i]==')')
		{
			you++;
		}
		if(zuo==you)
		{
			return 1;
		}
	}
	return 0;
}

int main()
{
	int n;
	scanf("%d", &n);

	getchar();

	int i;
	for(i=0;i<n;i++)
	{
		char c[200];
		scanf("%s", c);
		getchar();

		printf(c);

		printf("\n");

		int len=strlen(c);

		int j;

		for(j=0;j<len;j++)
		{
			if(c[j]=='(')
			{
				if(!zuok(c,j,len))
				{
					putchar('$');
				}
				else
					printf(" ");
			}

			else if(c[j]==')')
			{
				if(!youk(c,j,len))
				{
					putchar('?');
				}
				else
					putchar(' ');
			}

			else
			{
				putchar(' ');
			}

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


int main()
{	//int l,t=0;
	char string1[101];
	while(cin.getline(string1,101))
	{//t=0;

	cout<<string1<<endl;

	for(int i=1;i<strlen(string1);i++)
	{
		if(string1[i]==')')
	{
//			if(t==0)
//			{
//				l=i;t=1;
//			}
			for(int j=i-1;j>=0;j--)
			{
				if(string1[j]=='(')
				{	//l=j;
					string1[i]=' ';
					string1[j]=' ';
					break;
				}
			}
		}

	}
for(int k=0;k<strlen(string1);k++)
	{
		if(string1[k]=='(')
			cout<<"$";
		else if(string1[k]==')')
			cout<<"?";
		else
			cout<<" ";
	}
	cout<<endl;
	}
	return 0;
}


int main()
{
	char line[101];
	int length, i, j;
	while (cin.getline(line, 101))
	{
		length = strlen(line);
		for (i = 0; i < length; i++)
			cout << line[i];
		cout << endl;
		for (i = 0; i < length; i++)
		{
			if (line[i] == ')')
			{
				for (j = i; j >= 0; j--)
				{
					if (line[j] == '(')
					{
						line[i] = 0;
						line[j] = 0;
						break;
					}
				}
			}
		}
		for (i = 0; i < length; i++)
		{
			if (line[i] == '(')
				cout << "$";
			if (line[i] != '(' && line[i] != ')')
				cout << " ";
			if (line[i] == ')')
				cout << "?";
		}
		cout << endl;
	}
	return 0;
}


int findRight(char s[], int pos, int l){
	int t;
	if (s[pos]=='\0') return -1;
	if (s[pos]=='('){
		t=findRight(s,pos+1,l+1);
		if (t==-1){
			s[pos]='$';
			return -1;
		}else {
			s[pos]=' ';
			return findRight(s,t+1,l);
		}
	}else if (s[pos]==')'){
		if (l==0){
			s[pos]='?';
			findRight(s,pos+1,l);
		}
		else {
			s[pos]=' ';
			return pos;
		}
	}else {
		s[pos]=' ';
		return findRight(s,pos+1,l);
	}
}
int main(){
	char s[1000];
	for (;cin >> s;){
		cout<<s<<'\n';
		findRight(s,0,0);
		cout<<s<<'\n';
	}
}


int main()
{
	int i,j,flag[101] = {0};
	char str[101];
	while(cin >> str)
	{
	for (i = 0; i < strlen(str); i ++)
	{
		cout << str[i];
		if (str[i] == ')')
		{
			for (j = i; j >= 0; j --)
			{
				if (str[j] == '(' && flag[j] == 0)
				{
					flag[i] = 1;
					flag[j] = 1;
					break;
				}
			}
		}
	}
	cout << endl;
	for (i = 0; i < strlen(str); i ++)
	{
		if (str[i] == '(' && flag[i] == 0)
			cout << "$";
		else if (str[i] == ')' && flag[i] == 0)
			cout << "?";
		else
			cout << " ";
	}
	cout << endl;
	memset(flag,0,sizeof(flag));
	}
	return 0;
}


int main(){
  while(!cin.eof()){
    char str[MAX],ans[MAX];
    memset(str,0,sizeof(str));
    memset(ans,0,sizeof(ans));
    cin>>str;

    for(int i=0;i<strlen(str);i++) ans[i]=' ';

    int sign=0;
    for(int i=0;i<strlen(str);i++){
      if(str[i]=='('){

	sign++;
	ans[i]=sign+'0';
      }
      else if(str[i]==')'){
	if(sign>0){
	  ans[i]=sign+'0';
	  sign--;
	}
	else ans[i]='?';
      }
    }
    for(int i=0;i<strlen(str);i++){
      if(ans[i]!=' '&&str[i]=='('){
	int ok=0;
	int j;
	for(j=i+1;j<strlen(str);j++){
	  if(ans[j]==ans[i]){
	    ok=1;
	    break;
	  }
	}
	if(!ok) ans[i]='$';
	else ans[i]=ans[j]=' ';
      }
    }
    ans[strlen(str)]='\0';
    cout<<str<<endl;
    cout<<ans;
    cout<<endl;
  }
  return 0;
}


int f1(char zichuan[],int changdu)
{
    int i,n;
	for(i=changdu-1;i>=0;i--)
	{
		if(zichuan[i]=='(')
		{
			n=i+1;
		   while(zichuan[n]!=')')
		   {
			   n=n+1;
		   }
		   if(n<changdu)
		   {
			   zichuan[i]=' ';
			   zichuan[n]=' ';
		   }
		}
	}
	return 0;
}

int main()
{
	int i=0,changdu;
	char s[100];
  while(gets(s))
  {
  changdu=strlen(s);
  printf("%s\n",s);
  f1(s,changdu);
  for(i=0;i<changdu;i++)
  {
     switch(s[i])
	 {
	 case '(':s[i]='$';break;
	 case ')':s[i]='?';break;
	 default :s[i]=' ';break;
	 }
  }
   for(i=0;i<changdu;i++)
   {
	   printf("%c",s[i]);
   }
   printf("\n");
  }
	return 0;

}


void main()
{
	int n,k;
	char a[101];
	int i,j,flag;
	scanf("%d",&n);
	for(k=0;k<n;k++)
	{
		scanf("%s",a);
		printf("%s\n",a);
		for(i=0;i<strlen(a);i++)
		{
			if(a[i]==40)
			{
				if(i==strlen(a)-1)
					printf("$");
				else
				{
					flag=1;
					for(j=i+1;j<strlen(a);j++)
					{
						if(a[j]==41 && flag==1)
						{
							flag--;
							break;
						}
						else if (a[j]==41 && flag!=1)
						{
							flag=flag-1;
						}
						else if (a[j]==40)
						{
							flag=flag+1;
						}

					}
					if(flag==0)
						printf(" ");
					else
						printf("$");
				}
			}

			else if(a[i]==41)
			{
				if(i==0)
					printf("?");
				else
				{
					flag=1;
					for(j=i-1;j>=0;j--)
					{
						if(a[j]==40 && flag==1)
						{
							flag--;
							break;
						}
						else if (a[j]==40 && flag!=1)
						{
							flag=flag-1;
						}
						else if (a[j]==41)
						{
							flag=flag+1;
						}
					}
					if(flag==0)
						printf(" ");
					else
						printf("?");
				}
			}

			else
				printf(" ");
		}
		printf("\n");
	}

}


// 5.cpp : Defines the entry point for the console application.
//

int main()
{
	char s[101],ss[101];
	int len,i,j,q,h=0;
	while(gets(s)){
		len=strlen(s);
		strcpy(ss,s);
		for(i=0;i<len;i++){
			if(s[i]=='('){

				for(j=i+1;j<len;j++){
					if(s[j]=='('){
						i=j;
					}
					else if(s[j]==')'){
						s[i]=' ';
						s[j]=' ';
						break;
					}

				}
                                    if(j == len) break;

					i=-1;
			}
		}
		for(q=0;q<len;q++){
			if(s[q]==')'){
				s[q]='?';
			}
			else if(s[q]=='('){
				s[q]='$';
			}
			else
				s[q]=' ';
		}
		printf("%s\n",ss);
		printf("%s\n",s);
	}
	return 0;
}


char word[200];
int pp[200],len;
void op()
{
     int t=0,i;
     for(i=0;i<=len;i++)
     pp[i]=0;
     for(i=0;i<len;i++)
     {
                       if(word[i]=='(')
                       t++;
                       if(word[i]==')')
                       {
                                       t--;
                                       if(t<0)
                                       {pp[i]=ykhao;
                                       t++;}
                       }
     }
     t=0;
     for(i=len-1;i>=0;i--)
     {
                       if(word[i]==')'&&pp[i]!=ykhao)
                       t++;
                       if(word[i]=='(')
                       {
                                       t--;
                                       if(t<0)
                                       {pp[i]=zkhao;
                                       t++;}
                       }
     }
}
void pr()
{
     int i;
     printf("%s\n",word);
     for(i=0;i<len;i++)
     {
                        if(pp[i]==0)
                        printf(" ");
                        if(pp[i]==zkhao)
                        printf("$");
                        if(pp[i]==ykhao)
                        printf("?");
     }
}
/*int main()
{
    printf("1");
    getch();
    return 0;
}*/
int main()
{
    int n,i;
    //freopen("1.txt","w",stdout);
    scanf("%d\n",&n);
    gets(word);
    len=strlen(word);
    op();
    pr();
    for(i=2;i<=n;i++)
    {gets(word);
    len=strlen(word);
    printf("\n");
    op();
    pr();
    }
    //getch();
    return 0;
}


int main()
{
	char ans[100], c;
	int stack[100], top = 0, l = 0;
	while(scanf("%c", &c) == 1)
	{
		cout << c;
		if (c == '\n')
		{
			ans[l] = '\0';
			cout << ans << endl;
			top = 0;
			l = 0;
			continue;
		}
		if (c == '(')
		{
			stack[top++] = l;
			ans[l] = '$';
			l++;
			continue;
		}
		if (c == ')')
		{
			if (top == 0)
			{
				ans[l++] = '?';
				continue;
			}
			else
			{
				ans[l++] = ' ';
				ans[stack[--top]] = ' ';
				continue;
			}
		}
		ans[l++] = ' ';
	}
	cout << endl;
	ans[l] = '\0';
	cout << ans << endl;

	return 0;
}


int main()
{
	int i,j,k,left;
	char s[102]={'\0'},t[102]={'\0'};
	int num[102];
	while(scanf("%s",s)!=EOF)
	{
		printf("%s\n",s);
		k=strlen(s);
		left=0;
		for(i=0;i<=k-1;i++)
		{
			switch(s[i])
			{
			case 40:{left=left+1;num[left]=i;t[i]='$';break;}
			case 41:{if(left!=0){t[num[left]]=32;t[i]=32;left=left-1;}else {t[i]='?';}break;}
			default:{t[i]=32;break;}
			}
		}
		for(i=0;i<=k-1;i++)
		{printf("%c",t[i]);}
		printf("\n");
	}
	return 0;
}


right(char s[],int i,int n)
{
	int j;
	int left=0,right=0;
	int num=0;

	for(j=i+1;j<n;j++){
		if(s[j]=='(') right++;
		if(s[j]==')') left++;
		if(right<left) {num=1;break;}
	}
	return(num);
}

left(char s[],int i)
{
	int j;
	int left=0,right=0;
	int num=0;

	for(j=i-1;j>=0;j--){
		if(s[j]=='(') right++;
		if(s[j]==')') left++;
		if(right>left) {num=1;break;}
	}
	return(num);
}

trans(char s1[],char s2[])
{
	int len;
	int i;
	len=strlen(s1);

	for(i=0;i<len;i++){

			s2[i]=' ';

			if(s1[i]==')')
			{
				if(left(s1,i)==1) s2[i]=' ';
				else s2[i]='?';
			}

			if(s1[i]=='(')
			{
				if(right(s1,i,len)==1) s2[i]==' ';
				else s2[i]='$';
			}
	}
	s2[len]='\0';
	return;
}

main()
{
	int i,j;
	int n;
	struct aaa
	{
		char s1[105];
		char s2[105];
	};
	struct aaa line[20];

	scanf("%d\n",&n);

	for(i=0;i<n;i++){

		gets(line[i].s1);
		trans(line[i].s1,line[i].s2);
	}

	for(i=0;i<n;i++)
		printf("%s\n%s\n",line[i].s1,line[i].s2);
}


int main()
{
	while(1)
	{
		char str[101];
		int final[101]={0};
		cin.getline(str,101);
		if(str[0]=='\0') break;
		int i=0,p=0,q=0,k=0,flag=0;
		while(str[i]!='\0')
		{
			if(str[i]=='(') final[i]=1;
			if(str[i]==')')
			{
				final[i]=2;
				for(int j=i;j>=0;j--)
				{
					if(final[j]==1)
					{
						final[i]=0;
						final[j]=0;
						break;
					}
				}
			}
			i++;
		}
		cout<<str<<endl;
		while(str[k]!='\0')
		{
			if(final[k]==1||final[k]==2)
			{
				p=k;
				if(flag==0)
				{
					q=k;
					flag=1;
				}
			}
			k++;
		}
		for(int j=q;j<=p;j++)
		{
			if(final[j]==1) cout<<'$';
			else if(final[j]==2) cout<<'?';
			else cout<<' ';
		}
		cout<<endl;
	}
	return 0;
}


int main()
{
	char sen[101], rsen[101];
	while (cin >> sen)
	{
		int pos[101], num = 0, i, t = 0;
	cout <<  sen << endl;
	for (i = 0; i <= strlen(sen) - 1; i++)
	{
		if (sen[i] == '(')
		{
			num++;
			pos[num] = i;
			rsen[i] = '$';
		}
		else if (sen[i] == ')')
		{
			if (num > 0)
			{
				rsen[pos[num]] = ' ';
				rsen[i] = ' ';
				num--;
			}
			else
			{
				rsen[i] = '?';
			}
		}
		else
		{
			rsen[i] = ' ';
		}
	}
	if (num != 0)
	{
		for (i = 1; i <= num; i++)
		{
			rsen[pos[num]] = '$';
		}
	}
	for (i = 0; i <= strlen(sen) - 2; i++)
	{
		cout << rsen[i];
	}
	cout << rsen[strlen(sen) - 1] << endl;
	};
	return 0;
}


char str[120];int n;
void dispose(int x)
{
    int i,j,k=0;
    for(i=x+1,k=0;i<n;i++)
    {
        if(str[i]=='('&&k==0)
        {
            dispose(i);
            k++;

        }
        if(str[i]==')')
        {
            str[i]=' ';
            str[x]=' ';
            for(j=i+1;j<n;j++)
            {
                if(str[j]=='(')
                {
                    dispose(j);
                }
            }
            break;
        }
    }
}
main()
{
    int N,e;scanf("%d",&N);
    for(e=0;e<N;e++)
    {
        scanf("%s",str);printf("%s\n",str);
        int i,j,k;n=strlen(str);
        for(i=0;i<n;i++)
        {
            if(str[i]=='(') break;
        }
        dispose(i);//printf("%d\n",i);printf("%s\n",str);
        for(i=0;i<n;i++)
        {
            if(str[i]=='(') printf("$");
            else
            {
                if(str[i]==')')
                    printf("?");
                else
                    printf(" ");
            }
        }
        printf("\n");
    }
    }


int main()
{
    char str[101],label[101];
    while (cin >> str)
    {

    int pos[101];
    int len=strlen(str);
    int left=0,right=0;
    int pos_i=-1;
    int i;

    for (i=0;i<=len-1;i++)
    {
        label[i]=' ';
        if (str[i]=='(')
        {
            left++;
            pos[++pos_i]=i;
        }
        if (str[i]==')')
        {
            if (left<=right)
            {
                label[i]='?';
            }
            else
            {
                right++;
                pos_i--;
            }
        }
    }
   if (pos_i>=0)
   {
       for (i=0;i<=pos_i;i++)
       {
           label[pos[i]]='$';
       }
   }
   label[len]='\0';
   cout << str << endl;
   cout << label << endl;
}

   return 0;
}


int main()
{
    int i, j, f1=0, f2=0;
    char line[101], copy[101];
    while (cin >> line)
    {
	    strcpy(copy,line);
		for (i=0;i<strlen(line);i++)
	    {
			if (line[i]==')')
			    line[i]='?';
			if (line[i]=='(')
			{
				f1=1;
				for (j=i+1;j<strlen(line);j++)
				{
					if (line[j]=='(') f1++;
					if (line[j]==')')
					{
						f1--;
						if (f1==0)
						{
							line[i]=' ';
							line[j]=' ';
							break;
						}
					}
				}
				if (f1!=0)
					line[i]='$';
			}
		}
		cout << copy << endl;
		for (i=0;i<strlen(line);i++)
	    {
			if (line[i]=='$'||line[i]=='?') cout << line[i];
			else cout << " ";
		}
		cout << endl;
		memset(line, 0, sizeof(line));
		memset(copy, 0, sizeof(copy));
	}
	return 0;
}


int left(int x,char s[],int n);//???????x?????
int main()
{
	int l;
    char c[101]={0};
	gets(c);
	puts(c);
    l=left(0,c,0);
	printf("%s",c);
	while ((scanf("%s",c)!=EOF))
	{
	printf("\n");
	puts(c);
    l=left(0,c,0);
	printf("%s",c);
	}
	return 0;
}
int left(int x,char s[],int n)
{
	int a;
	if (s[n]!='\0')
	{
		if (s[n]=='(')
		{
			a=left(x+1,s,n+1);
			if (a>0)
			{
			s[n]=' ';
			return(a-1);
			}
           else
		   {
			   s[n]='$';
			   return 0;
		   }
		}
		else if (s[n]==')')
		{
			if (x>0)
			{
				s[n]=' ';
				return(left(x-1,s,n+1)+1);
			}
			else
            {
				s[n]='?';
				return(left(0,s,n+1)+1);
			}
		}
		else
		{
          s[n]=' ';
		  return left(x,s,n+1);
		}
	}
	else return 0;
}


int main()
{
	char line[101], label[101];
	int pos[100];
	int left = 0, right = 0;
	int pos_i, length;
	int i;
	while(cin.getline(line, sizeof(line)))
	{
		for(i = 0; i < 101; i++)
			label[i] = ' ';
		pos_i = -1, left = 0, right = 0;
		length = strlen(line);
		for(i = 0; line[i] != '\0'; i++)
		{
			if(line[i] == '(')
			{
				pos_i++;
				pos[pos_i] = i;
				left++;
			}
			if(line[i] == ')')
			{
				if(right >= left)
					label[i] = '?';
				else
				{
					right++;
					pos_i--;
				}
			}
		}
		if(pos_i >= 0)
		{
			for(i = 0; i <= pos_i; i++)
				label[pos[i]] = '$';
		}
		label[length] = '\0';
		cout << line << endl;
		cout << label << endl;
	}
	return 0;
}


int main()
{
	char a[200];
	int b[200];
	int n,len;
	cin>>n;
	cin.get();
	for (int i=0;i<n;i++)
	{
		memset(b,0,sizeof(b));
		cin.getline(a,200);
		len=strlen(a);
		for (int i=0;i<len;i++)
		{
			cout<<a[i];
			switch(a[i])
			{
			case '(':b[i]=1;break;
			case ')':b[i]=2;break;
			default :b[i]=3;break;
			}
		}
		cout<<endl;
		for (int i=0;i<len;i++)
		{
			if (b[i]==2)
			{
				for (int j=i;j>=0;j--)
				{
					if (b[j]==1)
					{
						b[j]=3,b[i]=3;
						break;
					}
				}
			}
		}
		for (int i=0;i<len;i++)
		{
			if (b[i]==1)
			{
				cout<<'$';
			}
			else if (b[i]==2)
			{
				cout<<'?';
			}
			else
			{
				cout<<' ';
			}

		}
		cout<<endl;
	}

	return 0;
}


int main()
{
	char str[110],tra[110],mark[110];
	int n,i,j,p,q;
	while(gets(str))
	{
		p=q=-1;
		n=strlen(str);
		mark[n]=NULL;
		for(i=0;i<n;i++)
		{
			if(str[i]=='(')
			    p=i;
		}
		strcpy(tra,str);
		for(i=p;i>=0;i--)
			if(tra[i]=='(')
				for(j=i+1;j<n;j++)
					if(tra[j]==')')
					{
						tra[i]=tra[j]='a';
						break;
					}
		for(i=0;i<n;i++)
			if(tra[i]=='(')
				mark[i]='$';
			else if(tra[i]==')')
				mark[i]='?';
			else mark[i]=' ';
		printf("%s\n%s\n",str,mark);
	}
	return 0;
}


int main(int argc, char *argv[])
{

  char str[200],str1[200];
  while(scanf("%s",str1)!=EOF)
  {
                         int m;
  m=strlen(str1);
  strcpy(str,str1);
  int i,j;
  for(i=1;i<m;i++)
  {
                  for(j=0;j<m;j++)
                  {
                                    if(i+j<m)
                                    {
                                             if(str[j]=='('&&str[j+i]==')')
                                             {
                                                                           str[j]='a';
                                                                           str[j+i]='a';
                                             }
                                    }
                  }
  }
  for(i=0;i<m;i++)
  {
                  if(str[i]=='(') str[i]='$';
                  else if(str[i]==')') str[i]='?';
                  else str[i]=' ';
  }
  printf("%s\n",str1);
  printf("%s\n",str);

}

  return 0;
}


int main(){
	char line[1000],label[1000];
	int pos[1000];
	int len,i;
	while(cin.getline(line,1000)){
		int left=0,right=0,s=-1;
		len=strlen(line);
		for(i=0;i<1000;i++)
			label[i]=' ';
		for(i=0;line[i]!='\0';i++)
		{
			if(line[i]=='(')
			{
				pos[++s]=i;left++;
			}
			if(line[i]==')'){
				if(left<=right)
					label[i]='?';
				else{
					right++; s--;
				}
			}
		}
		if(s>=0){
			for(i=0;i<=s;i++)
				label[pos[i]]='$';
		}
		label[len]='\0';
		cout<<line<<endl;
		cout<<label<<endl;
	}
	return 0;
}


/**
??????
*/

int main()
{
	char str[101], flag[101];
	while( cin.getline(str, 101))
	{
		cout << str << endl;
		int len, num = 0;
		len = strlen(str);
		int i, j;
		for ( i = 0; i < len; i ++ )
			flag[i] = ' ';
		flag[i] = '\0';
		for ( i = 0; i < len; i ++ )
		{
			if ( str[i] == '(')
			{
				num ++;
				flag[i] = '$';
			}
			if ( str[i] == ')')
			{
				num --;
				if ( num >= 0 )
				{
					for ( j = i - 1; j >= 0; j -- )
						if ( str[j] =='(' )
						{
							flag[j] = ' ';
							str[j] = ' ';
							break;
						}
				}
				else
				{
					flag[i] = '?';
					num ++;
				}
			}
		}
		cout << flag << endl;
	}
	return 0;
}


int main()
{
	char s[101];
	char kuohao[101]={0};
	int i,j,len;
	char ch;
	while(scanf("%s",s)>0){
		for(i=0; ;i++){
			if(s[i]=='(') kuohao[i]=1;
			else if(s[i]==')'){
				for(j=i;j>=0;j--){
					if(kuohao[j]==1){
						kuohao[j]=0;
						break;
					}
				}
				if(j<0) kuohao[i]=3;
			}
			else if(s[i]=='\0') break;
		}
	printf("%s\n",s);
	len=strlen(s);
	for(i=0;i<len;i++){
		if(kuohao[i]==0) printf(" ");
		else if(kuohao[i]==1) printf("$");
		else if(kuohao[i]==3) printf("?");
	}
	printf("\n");
	for(i=0;i<len;i++) kuohao[i]=0;
	}
	return 0;
}


int main()
{
	char s[101],ss[101];
	int len,i,j,q,h=0;
	while(gets(s))
	{
		len=strlen(s);
		strcpy(ss,s);
		for(i=0;i<len;i++)
			if(s[i]=='(')
			{
				h = i;
				for(j=i+1;j<len;j++)
					if(s[j]=='(')
						i=j;
					else if(s[j]==')')
					{
						s[i]=' ';
						s[j]=' ';
						break;
					}
				if(j == len) break;
				if(h == i) continue;
				else i = h-1;
			}
		for(q=0;q<len;q++)
			if(s[q]==')')
				s[q]='?';
			else if(s[q]=='(')
				s[q]='$';
			else
				s[q]=' ';
		printf("%s\n",ss);
		printf("%s\n",s);
	}
	return 0;
}


/*
 *???: brackets.cpp
 *??: ???
 *????: 2012-11-26
 *??: ????
 */

int brackets_matching(char str[],char result[],int n,int j)
/*???i?????str?????????result?
 * n?????.i????????????????????
 */
{
	int i,p;
	for (i=j;str[i];i++){ //???????
		switch (str[i]){
			case '(' : //???
			p=brackets_matching(str,result,n+1,i+1); //??????????
			if (str[p]==')') //????
				result[i]=' '; //???????????
			else
				result[i]='$'; //??????????p??\0
			i=p;
			break;

			case ')' : //???
			if (!n) //????
				result[i]='?';
			else{
				result[i]=' ';
				return i;
			}
			break;

			default :
			result[i]=' ';
		}
	}
	result[i]=0;
	return i;
}

int main()
{
	char a[MAXL],result[MAXL];
	while (cin>>a){
		cout << a << endl;
		brackets_matching(a,result,0,0);
		cout << result << endl;
	}
	return 0;
}


int main(void)
{
        char *exp=(char*)malloc(sizeof(char)*MAX_LEN);
        int *res=(int*)malloc(sizeof(int)*MAX_LEN);
        while(gets(exp))
        {
                int cur=0,pt=0,len=strlen(exp);
                for(int i=0;i!=MAX_LEN;++i)
                        res[i]=-1;
                for(;cur!=len;++cur)
                {
                        if(exp[cur]!='('&&exp[cur]!=')')
                                res[cur]=0;
                        if(exp[cur]==')')
                        {
                                for(pt=cur-1;pt>=0;--pt)
                                {
                                        if(exp[pt]=='('&&res[pt])
                                        {
                                                res[pt]=0;
                                                res[cur]=0;
                                                break;
                                        }
                                }
                        }
                }
                puts(exp);
                for(int i=0;i!=len;++i)
                {
                        if(exp[i]=='('&&res[i]==-1)
                                res[i]=1;
                        else if(exp[i]==')'&&res[i]==-1)
                                res[i]=2;
                        switch(res[i])
                        {
                        case 0:
                                putchar(' ');
                                break;
                        case 1:
                                putchar('$');
                                break;
                        case 2:
                                putchar('?');
                                break;
                        }
                }
                printf("\n");
        }
        return 0;
}


int main(int argc, char* argv[])
{
    int n,m,i,j;
	char str[101],str1[101];
	scanf("%d\n",&n);
	for(m=0;m<n;m++)
	{
      gets(str);
	  puts(str);
	  for(i=0;i<strlen(str);i++)
	  {
	    if(str[i]==')')
		for(j=i;j>=0;j--)
		   if(str[j]=='(')
		   {
			   str[j]='0';
			   str[i]='0';
			   break;
		   }
	  }

      printf("\n");
	  for(i=0;i<strlen(str);i++)
	  {
	   if(str[i]=='(')  printf("$");
	   if(str[i]==')')  printf("?");
	   if(str[i]!='('&&str[i]!=')')  printf(" ");
	  }
	  printf("\n");
	}
	return 0;
}


int main()
{
  char line[101],label[101];
  int pos[101];
  int n , j ;
  int left;
  cin >> n ;
cin.get();
  for( j = 1 ;j <= n ;j ++ )
  {
       left = 0 ;

       cin .getline(line,101);
       int len = strlen(line);
       for(int i = 0 ;i <= 100 ; i ++ )
       label[i]=' ';
       for( int i = 0 ;i <= len - 1; i ++ )
       {
                if(line[i] == '(')
                  {
                        left ++ ;
                        pos[left] = i ;
                  }
                   if(line[i] == ')')
                   {
                              if( left == 0)
                                label[i] ='?';
                               else
                                left -- ;
                   }
      }
      for( int i = 1 ;i <= left ; i ++ )
           label[pos[i]]='$';
           label[len]='\0';
           cout<< line << endl ;
           cout<< label << endl ;
  }return 0 ;
}


int main()
{
	char str[101];
	int temp;
	while(cin.getline(str,101,'\n'))
	{
		cout<<str<<endl;
		int k,i,s=0,t=0;
		char result[101];
		int left[101];
		memset(result,' ',sizeof(result));
		k=strlen(str);
		int p=0;
		for(i=0;i<k;i++)
		{
			if(str[i]=='(')
			{
				p++;
				left[s]=i;
				temp=s;
				s++;
			}
			if((str[i]==')')&&(p==0))
				result[i]='?';
			if((str[i]==')')&&(p!=0))
			{
				p--;
				left[temp]=-1;
				do
				{
					temp--;
				}
				while(left[temp]==-1);
			}
		}
		for(i=0;i<s;i++)
		{
			if(left[i]==-1)
				continue;
			else
				result[left[i]]='$';
		}
		for(i=0;i<k;i++)
			cout<<result[i];
		cout<<endl;
	}
	return 0;
}


int main()
{
    char c[100][110];
    int i,j,k,n;
    void pipei(char c[110]);
    int print(char c[110]);
    scanf("%d\n",&n);
    for(i=0;i<n;i++)
    {
                    gets(c[i]);
    }
    for(i=0;i<n;i++)
    {
                    printf("%s\n",c[i]);
                    pipei(c[i]);
    }
    return 0;
}

void pipei(char c[110])
{
     int p1=0,p2=0,tihuan=0,i,j,k;
     for(i=0;i<strlen(c);i++)
     {
                             if(c[i]!='(')
                             {
                                          p1+=1;
                                          p2+=1;
                             }
                             else break;
     }
     if(p1==strlen(c))
     {
                       print(c);
     }
     else
     {
         while(p1<strlen(c))
         {
                            if(c[p1]=='(')
                            {
                                          p2=p1;
                                          p1+=1;
                            }
                            else if(c[p1]==')')
                            {
                                 c[p1]='A';
                                 c[p2]='A';
                                 tihuan+=1;
                                 break;
                            }
                            else
                            {
                                p1+=1;
                            }
         }
         if(tihuan==0)
         {
                       print(c);
         }
         else pipei(c);
     }
}

int print(char c[110])
{
     int i;
     for(i=0;i<strlen(c);i++)
     {
                             if(c[i]=='(') printf("$");
                             else if(c[i]==')') printf("?");
                             else printf(" ");
     }
     printf("\n");
     return 0;
}


int num;
char right(char c)
{ char ch;
    if(c!='('&&c!=')')
       ch=' ';
    else if(c==')')
     { if (num==0) ch='?';
       else
        { num--;
          ch=' ';
        }
     }
    else
     { num++;
       ch=c;
     }
    return ch;
}
char left(char c,char a)
{ char ch;
  if(a==' '||a=='?')
   { ch=a;
     if(c==')')num++;
   }
  else if (num==0) ch='$';
       else
        { num--;
          ch=' ';
        }
  return ch;
}
main()
{ int i;
  char s[101],a[101];
  while(gets(s))
   { num=0;
     for(i=0;i<strlen(s);i++)
       a[i]=right(s[i]);
     num=0;
     for(i=strlen(s)-1;i>=0;i--)
       a[i]=left(s[i],a[i]);
     puts(s);
     for(i=0;i<strlen(s);i++)
       printf("%c",a[i]);
       printf("\n");
   }
  return 0;
}


int match (int);
int count (int);
int match (int,int,int,int);
char a[50][105],b[50][105]={0};
int num[50]={0};
int main ()
{
	for (int i=0;;i++){
		cin.getline (a[i],101);
		count (i);
		match (i);
		cout<<a[i]<<endl;
		cout<<b[i]<<endl;
	}
	return 0;
}
int count (int m)
{
	for (int i=0;a[m][i]!=0;i++)
		num[m]++;
	return 0;
}
int cur=0,flag=0;

int match (int k)
{
	for (int i=0;i<num[k];i++){
		if (a[k][i]=='(') b[k][i]='$';
		else if (a[k][i]==')') b[k][i]='?';
		else b[k][i]=' ';
	}
	while (1){
		int flag=-1,judge=0;
		for (int i=0;i<num[k];i++){
			if (b[k][i]=='$' && b[k][i+1]=='?')	{
			b[k][i]=' '; b[k][i+1]=' ';
			judge=1;break;
		}
		else if (b[k][i]=='$' && b[k][i+1]==' ')
			flag=i;
		else if (b[k][i]=='?' && flag>=0)
		{
			b[k][i]=' '; b[k][flag]=' ';
			 flag=-1;judge=1;break;
		}
			}
		if (judge==0)
			break;}
	return 0;

	}


/*
 * BracketCaptureIII.cpp
 *
 *  Created on: 2012-11-28
 *      Author: Cui Zhaoxiong Class4 1200012931
 */
int main() {
	char str[105];
	int flag = 0;
	int puma[105];
	while (cin >> str) {
		flag = 0;
		cout << str << endl;
		for (int i = 0; i <= strlen(str) - 1; i++) {
			if (str[i] == '(') {
				str[i] = '$';
				flag++;
				puma[flag] = i;
			} else if (str[i] == ')') {
				if (flag > 0) {
					str[puma[flag]] = ' ';
					str[i] = ' ';
					flag--;
				} else
					str[i] = '?';
			} else {
				str[i] = ' ';
			}
		}
		cout << str << endl;
	}
	return 0;
}


char * mystrrchr(char * strcopy, char * end)
{
    char * tmp = end-1;
    while(tmp>=strcopy)
    {
        if(*tmp=='(')
        {
            return tmp;
        }
        tmp--;
    }
    return NULL;
}

void judge(char * str,char * flag)
{
    char strcopy[101];
    strcpy(strcopy,str);
    int len=strlen(strcopy);
    char * index1=strchr(strcopy,')');
    while(index1!=NULL&&(index1-strcopy)<len)
    {
        char * index2=mystrrchr(strcopy,index1);
        if(index2!=NULL)
        {
            strcopy[index1-strcopy]='-';
            strcopy[index2-strcopy]='-';
        }
        index1=strchr(index1+1,')');
    }
    int i=0;
    for(i=0;i<len;i++)
    {
        if(strcopy[i]=='(')
            flag[i]='$';
        if(strcopy[i]==')')
            flag[i]='?';
    }
    printf("%s\n%s\n",str,flag);
}

int main()
{
    int n=0;
    scanf("%d",&n);
    getchar();
    char str[101];
    char flag[101];
    while(n>0)
    {
        memset(flag,' ',sizeof(flag));
        gets(str);
        flag[strlen(str)]='\0';
        judge(str,flag);
        n--;
    }
    return 0;
}


int len;
char str[101];
void f ( int p ) {
	int m = -1 , n = -1;
	for ( int i = p; i >= 0; i -- ) {
		if (*( str + i ) == '(') {
			m = i;
			break;
		}
	}
	for ( int i = m + 1; i <= len - 1; i ++ ) {
		if (*( str + i ) == ')')  {
			n = i;
			break;
		}
	}
	if ( m >= 0 && n >= 0 ) {
	str[m] = '#';
	str[n] = '#';
	}
	if ( m > 0 ) {
		f ( m - 1 );
	}
}

int main() {
	while( cin >> str ) {
	len = strlen( str );
	cout << str << endl;
	f ( len );
	for ( int j = 0;j <= len - 1; j ++ ) {
		if ( str[j] == '(' ) cout << "$";
		else if ( str[j] == ')' ) cout << "?";
		else cout << " ";
	}
	cout <<  endl;
	}
	return 0;
}


int main()
{
	char ch[110],temp[110],c[110];
	int i,figure;
	while(gets(ch)){
		figure = 0;
		for(i=0;ch[i] != '\0';i ++)
		{  temp[i]=' ';
		switch(ch[i]){
case '(':c[figure ++]=i;temp[i]='$';break;
case ')':if(figure){figure--;temp[c[figure]]=' ';
		 }
		 else temp[i] = '?';break;}
		}
		temp[i]='\0';
		printf("%s\n%s\n",ch,temp);
	}
	return 0;
}


int main(int argc, char* argv[])
{
	int N;cin>>N;
	for(int I=0;I<N;I++)
	{
		int i;
		char in[110];cin>>in;

		cout<<in<<endl;
		int n=strlen(in);

		char mark[110]; for(i=0;i<n;i++) mark[i]=' ';

		int left=0;

		for(i=0;i<n;i++)
		{
			if(in[i]=='(') left++;

			else if(in[i]==')')
			{
				if(left)
				{left--;}
				else
				{
					mark[i]='?';
				}
			}

		}

		int right=0;
		for(i=n-1;i>=0;i--)
		{
			if(in[i]==')') right++;

			else if(in[i]=='(')
			{
				if(right)
				{right--;}
				else
				{
					mark[i]='$';
				}
			}

		}

		for(i=0;i<n;i++) cout<<mark[i];
		cout<<endl;
	}

	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 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;
}


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;
}


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;
}


char num2[110] = {'\0'};

int left(char num[110], int point, int sum, int l) {   // check if it is a wrong '('
	if(point == l) { // finish the function
		if(num[point] == '(') {
			num2[point] = '$';
			return 0;
		}
		if(num[point] == ')') {
			sum = 1;
			return sum;
		}
		if(num[point] != '(' && num[point] != ')') return 0;
	}
	else {
		int k = left(num, point + 1, sum, l);
		if(num[point] == '(' && k == 0) {
			num2[point] = '$';
			return 0;
		}
		if(num[point] == '(' && k != 0) {
			sum = k - 1;
			return sum;
		}
		if(num[point] == ')') {
			sum = k + 1;
			return sum;
		}
		if(num[point] != '(' && num[point] != ')')
			return k;
	}
}

int right(char num[110], int point, int sum, int l) {   // check if it is a wrong '('
	if(point == l) { // finish the function
		if(num[point] == ')') {
			num2[point] = '?';
			return 0;
		}
		if(num[point] == '(') {
			sum = 1;
			return sum;
		}
		if(num[point] != '(' && num[point] != ')') return 0;
	}
	else {
		int k = right(num, point - 1, sum, l);
		if(num[point] == ')' && k == 0) {
			num2[point] = '?';
			return 0;
		}
		if(num[point] == ')' && k != 0) {
			sum = k - 1;
			return sum;
		}
		if(num[point] == '(') {
			sum = k + 1;
			return sum;
		}
		if(num[point] != '(' && num[point] != ')')
			return k;
	}
}

int main() {
	char num[110] = {'\0'};
	while(cin >> num) {
	int l = strlen(num);   // store the total length of the num
	cout << num << endl;
	for(int i = 0; i < l; i++) // build a new num to show the wrong '(' and ')'
		num2[i] = ' ';
	num2[l] = '\0';
	left(num, 0, 0, l - 1);
	right(num, l - 1, 0, 0);
	cout << num2 << endl;
	}

	return 0;
}


int main(){
char chuan[101];
while(scanf("%s",chuan)!=EOF){
    printf("%s\n",chuan);
    int i,j,k,p;
    int sig=0;
    p=strlen(chuan)-1;
    for(i=p;i>=0;i--){
        if(chuan[i]==')') sig++;
        if((chuan[i]!='(')&&(chuan[i]!=')')) chuan[i]=' ';
        if((sig!=0)&&(chuan[i]=='(')){
            chuan[i]=' ';sig=sig-1;
            for(j=i+1;j<=p;j++)
                if(chuan[j]==')'){
                    chuan[j]=' ';break;}}}
    for(i=0;i<=p;i++){
        if(chuan[i]=='(') printf("$");
        else if(chuan[i]==')') printf("?");
        else printf(" ");}
    printf("\n");
}
return 0;
}


int main()
{
    char str[1001];int n;
    while(~scanf("%s",str))
    {
        n=strlen(str);
        printf("%s",str);
        printf("\n");
        for(int i=1;i<n;i++)
        {
            if(str[i]==')')
            {
                for(int j=i-1;j>=0;j--)
                {if(str[j]=='(') {str[i]=' ';str[j]=' ';break;}}
            }
        }
        for(int i=0;i<n;i++)
        {
            if(str[i]=='(') str[i]='$';
            else if(str[i]==')') str[i]='?';
            else str[i]=' ';
        }
        printf("%s",str);
        printf("\n");
    }
}


char a[200];
void match(char a[])
{

	int s;
	s=strlen(a);
	if(s==0) return;
	int i,j;
	int check=0;
	for(i=0;i<s-1;i++)
	{
		if(a[i]=='(')
		{
			for(j=i+1;j<s&&a[j]==' ';j++);
			if(j<s)
			{
				if(a[j]==')')
				{
					check=1;
					break;
				}
			}
		}
	}
	if(check==1)
	{
		a[i]=' ';
		a[j]=' ';
		match(a);
	}
	if(check==0)
		return;
}
int main()
{

	int n;
	scanf("%d",&n);
	char temp;
	scanf("%c",&temp);
	while(n--)
	{

		gets(a);
		puts(a);
        int s;
	    s=strlen(a);
		int i;
		for(i=0;i<s;i++)
			if(a[i]!='('&&a[i]!=')') a[i]=' ';
		match(a);
		for(i=0;i<s;i++)
		{
			if(a[i]=='(') a[i]='$';
			else if(a[i]==')') a[i]='?';
		}
		puts(a);
	}
	return 0;
}


int main()
{
     void match(char *ps, char *pf, int len);
     char a[N] = {'\0'};
     while( scanf("%s", a) == 1) {
          char b[N] = {'\0'};
          match(a, b, strlen(a));
     }
     return 0;
}

void match(char *ps, char *pf, int len)
{
     int i, flag, last, isleft, isright;
     char str[N] = {'\0'}, strf[N] = {'\0'};
     strcpy(strf, pf);
     strcpy(str, ps);
     for(i = 0; i < len; i++) {
          if(*(str + i) == '(')
               *(strf + i) = -1;
          else if( *(str + i) == ')' )
               *(strf + i) = +1;
     }

     do {
          flag = 0;
          last = 0;
          isleft = 0;
          isright = 0;
          for(i = 0; i < len; i++) {
               if (*(strf + i) == 1 && flag == 1) {
                    *(strf + i) = 0;
                    *(strf + last) = 0;
                    flag = 0;
                    isleft = 1;
               } else if(*(strf + i) == -1) {
                    flag = 1;
                    last = i;
                    isright = 1;
               }
          }
     } while(isleft && isright);
     printf("%s\n", ps);
     for(i = 0; i < len; i++) {
          if (*(strf + i) == -1)
               printf("$");
          else if (*(strf + i) == +1)
               printf("?");
          else
               printf(" ");
     }
     printf("\n");
}


int main()
{
	char c[110],s[110];
	int left,i;
	int leftp[110]={0};
	while (cin >>c)
	{
		left=0;i=0;
		memset(leftp,0,110*sizeof(int));
		memset(s,0,110*sizeof(char));
		while (c[i]!='\0')
		{
			if (c[i]=='(')
			{
				left++;leftp[left]=i;
			}
			else
				if (c[i]==')')
				{
					if (left==0)
						s[i]='?';
					else
					{
						left--;
					}
				}
			i++;
		}
		if (left>0)
		    for (i=1;i<=left;i++)
				s[leftp[i]]='$';
		cout <<c<<endl;
		for (i=0;i<strlen(c);i++)
		    if ((s[i]=='?')||(s[i]=='$'))
				cout <<s[i];
			else
				cout <<' ';
		cout <<endl;
	}
	return 0;
}


char in[102];
int lenth;
void match(int n)
{
     for(int i=0;i<lenth;i++)
     {
             if(in[i]=='('&&in[i+n]==')')
             {
                  in[i]=' ';
                  in[i+n]=' ';
             }
     }
     if(n<=lenth-2)
     {
     match(n+1);
     }
}
void print(char in[])
{
     for(int i=0;i<lenth;i++)
     {
             if(in[i]=='(')
                  in[i]='$';
             else if(in[i]==')')
                  in[i]='?';
             else
                  in[i]=' ';
     }
     cout<<in<<endl;
}
int main()
{
    while(cin>>in)
    {
           cout<<in<<endl;
           lenth=strlen(in);
           match(0);
           print(in);
    }
    return 0;
}


void Match(int input_len);
char input[128];
char tmp_input[128];
int main()
{
	//freopen("test.txt","r",stdin);
	while(scanf("%s",input)!=EOF)
	{
		strcpy(tmp_input,input);
		int input_len=strlen(input);
		Match(input_len);
		for(int i=0;i<=input_len-1;i++)
		{
			if(input[i]=='(')
			{
				input[i]='$';
			}
			else if(input[i]==')')
			{
				input[i]='?';
			}
			else
			{
				input[i]=' ';
			}
		}
		printf("%s\n",tmp_input);
		printf("%s\n",input);
	}
	return 0;
}
void Match(int input_len)
{
	int cur_left=-1,cur_right=-1;
	for(int i=0;i<=input_len-1;i++)
	{
		if(input[i]=='(')
		{
			cur_left=i;
			continue;
		}
		if(input[i]==')')
		{
			if(cur_left!=-1)
			{
				input[cur_left]=' ';
				input[i]=' ';
				cur_left=-1;
				i=-1;
				continue;
			}
		}
	}
}


char a[150];int l,j;
void left(int i){
    int jj;
    /*
    cout << "left" << i << endl;
    for (int j = 0; j < l; j++) {
        cout << a[j];
    }
    cout << endl;*/
	if(i==l-1){a[i]='$';return;}
	for(jj=i+1;jj<l;jj++){
        //cout << "for jj=" << jj << endl;
		if(a[jj]==')'){
            a[jj]=' ';
            a[i]=' ';break;
		}
		else if(a[jj]=='('){left(jj);}
    }
    if (jj == l) {
        a[i] = '$';
    }
    //cout << "return" << endl;
	return;
}
void match(int i){//if(i==0){for(int k=0;k<l;k++)cout<<a[k];return;}
    /*cout << i << endl;
    for (int j = 0; j < l; j++) {
        cout << a[j];
    }
    cout << endl;*/
	if(i==l){
		for(int ii=0;ii<l;ii++){
			if(a[ii]=='('){a[ii]='$';}
			if(a[ii]==')'){a[ii]='?';}
		}
		return;
	}
	else{
		if(i==l-1&&a[i]=='('){a[i]='$';}
		if((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z')){a[i]=' ';}
		else if(a[i]=='('){left(i);}
		match(i+1);
	}
}
int main(){
	while(cin >> a){
		l=strlen(a);for(int k=0;k<l;k++)cout<<a[k];cout<<endl;
		match(0);for(int k=0;k<l;k++)cout<<a[k];cout<<endl;
	}
	return 0;
}


int main()
{
    char zfc[105];
    char sign[105];
    char present[105];
    while(scanf("%s",zfc)>0)
    {
        //scanf("%s",zfc);
        strcpy(sign,zfc);
        strcpy(present,zfc);
        int left=0,right=0;
        int m=strlen(zfc);
        for(int i=0;i<m;i++)
        {
            if(zfc[i]=='(')
            {
               left=0;
               right=0;
               for(int j=i;j<m;j++)
               {
                   if(zfc[j]=='(')
                   {
                      left++;
                   }
                   if(zfc[j]==')')
                   {
                      left--;
                   }
                   if(left<=0)
                   {
                      break;
                   }
               }
               if(left>0)
               {
                  sign[i]='$';
               }
            }
            if(zfc[i]==')')
            {
               left=0;
               right=0;
               for(int j=i;j>=0;j--)
               {
                   if(zfc[j]=='(')
                   {
                      right--;
                   }
                   if(zfc[j]==')')
                   {
                      right++;
                   }
                   if(right<=0)
                   {
                      break;
                   }
               }
               if(right>0)
               {
                  sign[i]='?';
               }
            }
        }
        for(int i=0;i<m;i++)
        {
            if(sign[i]!='$'&&sign[i]!='?')
            {
               sign[i]=' ';
            }
        }
        puts(present);
        puts(sign);
    }
    return 0;
}


int main()
{
	/*typedef struct _Parenthesis
	{
		char ch;
		int idx;
	} Parenthesis;*/
	int LeftParenIndex[100];
	char str[101];
	while(gets(str))
	{
		int i,top=0;
		puts(str);
		for(i=0; str[i]; i++)
		{
			if(str[i] == '(')
				LeftParenIndex[top++] = i;
			else if(str[i] == ')')
			{
				if(top == 0)
					str[i] = '?';
				else
				{
					str[i] = ' ';
					str[LeftParenIndex[--top]] = ' ';
				}
			}
			else
				str[i] = ' ';
		}
		for(i=0; i<top; i++)
			str[LeftParenIndex[i]] = '$';
		puts(str);
	}
	return 0;
}


int main()
{
	char input[101]={0},initial[101];
	int found=0;//????????
	while (cin.getline(input,101))
	{
	strcpy(initial,input);
	do
	{
		found=0;
		int i=0,location=-1;
		while (input[i])
		{
			if (input[i]=='(')
			{
				location=i;
				found=1;
			}
			if (input[i]==')')
			{
				found=1;
				if (location==-1) input[i]='?';
				else
				{
					input[location]=' ';
					input[i]=' ';
					location=-1;
					break;
				}
			}
			if (input[i]!='?'&&input[i]!='$'&&input[i]!='('&&input[i]!=')') input[i]=' ';
			i++;
		}
		if (location!=-1)
			{
				input[location]='$';
				location=-1;
			}
	}
	while (found);
	cout <<initial <<endl <<input <<endl;
	}
	return 0;
}


struct bracket
{
    char data[max];
    int top;
};

int main()
{
    char str[max]={0}, flag[max]={0};
    int i, len,n;
    struct bracket *leftbracket, *rightbracket;
  scanf("%d",&n);
while(n>0)
    {while(scanf("%s", str) != EOF)
    {
        //????
        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;
            }
        }

        //????
        printf("%s\n%s\n", str, flag);

        //?????
        memset(str, '\0', sizeof(str));
        memset(flag, '\0', sizeof(flag));
    }
n--;}
    return 0;
}


int main()
{
    char word[105],sign[105];
    int num=0,i,len;
    while(cin.getline(word,105))
    {
           num=0;
       len=strlen(word);
    for(i=0;i<len;i++)
    sign[i]=' ';
    for(i=0;i<len;i++)
    {
           if(word[i]=='(')
              num++;
           if(word[i]==')')
           {
                  if(num>0)
                  num--;
                  else sign[i]='?';
           }
    }
    num=0;
    for(i=len-1;i>=0;i--)
    {
           if(word[i]==')')
              num++;
           if(word[i]=='(')
           {
                  if(num>0)
                  num--;
                  else sign[i]='$';
           }
    }
    sign[len]='\0';
    cout<<word<<endl;
    cout<<sign<<endl;
    }
    return 0;
}


int function(char* out,int t)
{
	int i=t;
	if(out[i]=='(')
	{
		out[i]='$';
		i++;
		while(out[i]!=')'&&out[i])
		{
			if(out[i]=='(')
			{
				i=function(out,i)+1;
				if(i==-1)return -2;
			}
			else
			{
				out[i]=' ';
			    i++;
			}
		}
		if(out[i]==')')
		{
			out[t]=out[i]=' ';
			return i;
		}
		else return -2;
	}
	else
	{
		while(out[i]!='('&&out[i])
		{
			if(out[i]==')')
				out[i]='?';
			else
			    out[i]=' ';
			i++;
		}
		if(out [i]=='(')
			return function(out,i);
		else return -2;
	}
}

int main()
{
	char out[101];
	int n=-1;
	cin.getline(out,101);
	while(out[0])
	{
		n=-1;
		cout<<out<<endl;
		while(n!=-2)
		{
			n=function(out,n+1);
		}
		cout<<out<<endl;
		cin.getline(out,101);
	}
	return 0;
}


int main() {
	int n;
	cin >> n;
	for ( int m = 1; m <= n; m ++ ) {
		char str[105], str2[105];
		cin >> str;
		int stack[105];
		int len = strlen( str ), k = 0;
		for ( int i = 0; i < len; i ++ ) {
			str2[i] = ' ';
		}
		str2[len] = '\0';
		for ( int i = 0; i < len; i ++ ) {
			if ( str[i] == ')' ) {
				if ( k == 0 || stack[k - 1] > 0 ) {
					stack[k ++] = i + 1;
				}
				else k --;
			}
			else if ( str[i] == '(' ) {
				stack[k ++] = - i - 1;
			}
		}
		for ( int i = 0; i < k; i ++ ) {
			if ( stack[i] < 0 ) str2[-stack[i] - 1] = '$';
			else str2[stack[i] - 1] = '?';
		}
		cout << str << endl;
		cout << str2 << endl;
	}
	return 0;
}


char st[100];
char st2[100];
int main()
{
	memset(st,0,100);
	memset(st2,0,100);
	while(cin>>st)
	{
		int left=0,right=0;
		int len=strlen(st);
		for(int i=0;i<len;i++)
		{
			if(st[i]=='(')
				left++;
			if(st[i]==')')
				right++;
			if(left<right)
			{
				st2[i]='?';
				right--;
			}
		}
		left=0;
		right=0;
		for(int j=len-1;j>=0;j--)
		{
			if(st[j]=='(')
				left++;
			if(st[j]==')')
				right++;
			if(left>right)
			{
				st2[j]='$';
				left--;
			}
		}
		for(int i=0;i<len;i++)
		{
			if(st2[i]!='$'&&st2[i]!='?')
				st2[i]=' ';
		}
		cout<<st<<endl;
		cout<<st2<<endl;
		memset(st,0,100);
		memset(st2,0,100);
	}
	return 0;
}


int main(){
    int i,j,flag,q;
    char str[101],note[101];

    while(scanf("%s",str)!=EOF){
        strcpy(note,str);

        for(i=0;i<strlen(note);i++){
            if((note[i]>='a'&&note[i]<='z')||(note[i]>='A'&&note[i]<='Z'))
                note[i]=' ';
        }

        for(i=0;i<strlen(note);i++){
            flag=0;
            if(note[i]==')'){
                for(j=i-1;j>=0;j--){
                    if(note[j]=='('){
                        flag=1;
                        note[j]=' ';
                        note[i]=' ';
                        break;
                    }
                }
                if(flag==0)
                    note[i]='?';
            }
        }

        for(i=0;i<strlen(note);i++){
            if(note[i]=='(')
                note[i]='$';
        }

        puts(str);
        puts(note);
    }

    scanf("%d",&q);
    return 0;
}


int main()
{
	int n;
	char str1[120];
	char str[120];
	cin>>n;

	for (int i=1;i<=n;i++)
	{
		memset(str,'\0',sizeof(str));
		cin>>str1;
		strcpy(str,str1);

		for ( int len=1;len<strlen(str);len++)
		{
			for ( int j=0;j<strlen(str)-len;j++)
			{
				int k=j+len;
				if ( str[j]=='(' && str[k]==')')
				{
					str[j]='0';
					str[k]='0';
				}
			}
		}

		for ( int i1=0;str[i1]!='\0';i1++)
		{
			if ( str[i1]=='(')
				str[i1]='$';
			else if ( str[i1]==')')
				str[i1]='?';
			else str[i1]=' ';
		}
		cout<<str1<<endl;
		cout<<str<<endl;
	}
	return 0;
}


int num;
char right(char c)
{ char ch;
    if(c!='('&&c!=')')
       ch=' ';
    else if(c==')')
     { if (num==0) ch='?';
       else
        { num--;
          ch=' ';
        }
     }
    else
     { num++;
       ch=c;
     }
    return ch;
}
char left(char c,char a)
{ char ch;
  if(a==' '||a=='?')
   { ch=a;
     if(c==')')num++;
   }
  else if (num==0) ch='$';
       else
        { num--;
          ch=' ';
        }
  return ch;
}
main()
{ int i,n;
  char s[101],a[101];
  scanf("%d",&n);
  while(gets(s))
   { num=0;
     for(i=0;i<strlen(s);i++)
       a[i]=right(s[i]);
     num=0;
     for(i=strlen(s)-1;i>=0;i--)
       a[i]=left(s[i],a[i]);
     puts(s);
     for(i=0;i<strlen(s);i++)
       printf("%c",a[i]);
       printf("\n");
   }
  return 0;
}


char s[105];
int len;
int f(int b) {
	for (int i = b + 1; i < len; i++) {
		if (s[i] == ')') {
			s[b] = '$';
			s[i] = '?';
			return i;
		} else if (s[i] == '(') {
			i = f(i);
		}
	}
	return len - 1;
}
int main() {
	while (cin >> s) {
		len = strlen(s);
		cout << s << endl;
		for (int i = 0; i < len; i++) {
			if (s[i] == '(')
				i = f(i);
		}
		for (int i = 0; i < len; i++) {
			if (s[i] == '(')
				cout << '$';
			else if (s[i] == ')')
				cout << '?';
			else
				cout << ' ';
		}
		cout << endl;
	}
	return 0;
}


char str[101], bracket[101];
int len;
void match(int a);

int main()
{
    while (cin.getline(str, 101))
    {
        len = strlen(str);
        for (int i = 0; i < len; i++)
            if (str[i] == '(') bracket[i] = '$';
            else if (str[i] == ')') bracket[i] = '?';
            else bracket[i] = ' ';
        match(1);
        for (int i = 0; i < len; i++)
            cout << str[i];
        cout << endl;
        for (int i = 0; i < len; i++)
            cout << bracket[i];
        cout << endl;
    }

    return 0;
}

void match(int a)
{
     if (a == len) return;
     int b = a - 1;
     if (bracket[a] == '?')
     {
         while (bracket[b] == ' ')
         {
             if (b == 0)
             {
                 match(a + 1);
                 return;
             }
             b--;
         }
         if (bracket[b] == '?')
         {
             match(a + 1);
             return;
         }
         bracket[b] = ' ';
         bracket[a] = ' ';
         match(a + 1);
     }
     else match(a + 1);
}


char s2[101];

int ma(char s[],int p,int le)
{
	int ri;
	if (s[p]==0)
	{
		s2[p]=0;
		return 0;
	}
	else
	{
		if (s[p]=='(')
		{
			ri=ma(s,p+1,le+1);
			if (ri)
			{
				s2[p]=' ';
				return ri-1;
			}
			else
			{
				s2[p]='$';
				return 0;
			}
		}
		else if (s[p]==')')
		{
			if (le)
			{
				s2[p]=' ';
				return ma(s,p+1,le-1)+1;
			}
			else
			{
				s2[p]='?';
				return  ma(s,p+1,0)+1;
			}
		}
		else
		{
			s2[p]=' ';
			return ma(s,p+1,le);
		}
	}
}

int main()
{
	int n;
	scanf("%d\n",&n);
	char s1[101];
	int t;
	while(n)
	{
		n=n-1;
	gets(s1);
	puts(s1);
	t=ma(s1,0,0);
	printf("%s",s2);
	if (n) printf("\n");
	}
	return 0;
}


void pipei(char str[])
{
	int i,la,stack[100],k=0;
	char res[101];
	la=strlen(str);
	for(i=0;i<la;i++) res[i]=' ';
	res[la]='\0';
	for(i=0;i<la;i++)
	{
		if(str[i]==')')
		{
			if(k==0||stack[k-1]>0)
				stack[k++]=i+1;
			else k--;
		}
		else if(str[i]=='(')
			stack[k++]=-(i+1);
	}
	for(i=0;i<k;i++)
	{
		int a=stack[i];
		if(a<0) res[-a-1]='$';
		else res[a-1]='?';
	}
	cout<<str<<endl;
	cout<<res<<endl;
}
int main()
{
	int n,i;
	cin>>n;
	cin.get();
	char str[101];
	for(i=0;i<n;i++)
	{

        cin.getline(str,101);
		pipei(str);

	}
	return 0;
}


const int maxn = 110;

int main()
{
    char str[maxn],str2[maxn];
    while(scanf("%s",str)!=EOF){
        int len = strlen(str);
        for(int i = 0 ;i < len; i++)str2[i]=str[i];
        for(int  i = 0; i < len; i ++){
            if(str[i]==')'){
                for(int  j  = i-1; j >=0; j--){
                    if(str[j]=='(') {
                        str[j]=str[i]=' ';
                        break;
                    }
                }
            }
        }
        for(int i =0 ; i <len; i++) printf("%c",str2[i]);
        printf("\n");
        for(int i = 0; i < len; i++){
            if(str[i]=='(') printf("$");
            else if(str[i]==')') printf("?");
            else printf(" ");
        }
        printf("\n");
    }

    return 0;
}


void Replace(char s[],int x)
{
	for(int i=x+1;i<strlen(s);i++)
	{
	if(s[i]==')'){s[x]=s[i]=' ';return;}
	if(s[i]=='(')Replace(s,i);
	}
	s[x]='$';
return;
}
void Find(char s[])
{
	for(int i=0;i<strlen(s);i++)
	{
if(s[i]=='$'||s[i]=='?')continue;
		if(s[i]=='(')Replace(s,i);
		else if(s[i]==')')s[i]='?';
		else {
			s[i]=' ';
		}
	}
	return;
}

int main()
{
	char live[101];//string live;
int n;cin>>n;
	while(n--)
	{
cin>>live;
		cout<<live<<endl;
		Find(live);
		cout<<live<<endl;
	}
	return 0;
}


int main()
{
    char s[120];
    while(cin>>s)
{
    char res[120];//result
    int lens;
    int leftnum=0;//remaining leftnum
    int left[120];
    lens=strlen(s);
    for(int i=0;i<lens;i++)
        res[i]=' ';
    for(int i=1;i<120;i++)
      left[i]=-1;
    for(int i=0;i<lens;i++)
    {
        if( s[i]=='(' )
            left[++leftnum]=i;
        if( s[i]==')' )
        {
            if(leftnum==0)
              res[i]='?';
            else
            {
              left[leftnum]=-1;
              leftnum--;
            }
        }
    }
    for(int i=1;i<=leftnum;i++)
        res[left[i]]='$';
    for(int i=0;i<lens;i++)
        cout<<s[i];
    cout<<endl;
    for(int i=0;i<lens;i++)
        cout<<res[i];
    cout<<endl;
}

    return 0;
}


int main()
{
	int l , r , fl , lenth ;
	int i , j , k ;
	char str[100 + 10];
    int left[101] , right[101];
	int flag[100 + 10];

	while(cin.getline(str, 101))
	{
		l = 1; r = 1;
		for( lenth = 0 ; str[lenth] != '\0' ; lenth ++)
		{
			if( str[lenth] == '(' )
			{
				flag[lenth] = 1;
				left[l ++] = lenth;
			}
			else if( str[lenth] == ')' )
			{
				flag[lenth] = 2;
				right[r ++] = lenth;
			}
			else 	flag[lenth] = 0;
		}

		fl = lenth - 1;

		for ( j = l - 1 ; j >= 1 ; j --)
			for(k = 1 ; k <= r - 1 ; k ++)
			{
				if(right[k] > left[j])
				{
					flag[ right[k] ] = 0;
					flag[ left[j] ] = 0;
					right[k] = -1;
					break;
				}
			}

		for( lenth = 0 ; str[lenth] != '\0' ; lenth ++)
			printf("%c",str[lenth]);

			printf("\n");

		/*for( j = fl ; j >= 0; j --)
			if(flag[j] != 0) break; */

		/*for( i = 0 ; i <= fl ; i ++)
			if(flag[i] != 0 ) break;*/

		for( lenth = 0 ; lenth <= fl  ; lenth ++)
		{
			if(flag[lenth] == 0)     printf(" ");
		    else if(flag[lenth] == 1)	printf("$");
			else if(flag[lenth] == 2)	printf("?");
        }

        printf("\n");
		memset(str,0,sizeof(str));
	}

	return 0;
}


char s[1000];
char m[1000];
int match(int l){
	//º¯Êý·µ»ØÕÒµ½µÄÓÒÀ¨ºÅµÄÏÂ±ê
	int r = l;
	while(s[++r]!=')'){
		if(s[r]=='(')r=match(r);
		if(s[r]=='\0')break;
	}
	if(s[r]=='\0')m[l]='$';
	return r;
}
int nextL(int i){
	while(s[i]!='\0' && s[i]!='('){
		if(s[i]==')')m[i]='?';
		i++;
	}
	return i;
}
int main(){
	int i,j,k;
	while(cin>>s){
		for(i=0;i<strlen(s);++i)m[i]=' ';
		m[i]='\0';
		i=0;
		i=nextL(i);
		while(s[i]!='\0'){
			i=match(i);
			if(s[i]=='\0')break;
			else i++;
			i=nextL(i);
		}
		cout<<s<<endl<<m<<endl;
	}
	return 0;
}


int main(){
	char str[101], res[101];
	int n, stack[100], k;

	cin >> n;
	while (n--){
		cin >> str;
		int len = strlen(str);
		for (int i = 0; i < len; i++)
			res[i] = ' ';
		res[len] = '\0';

		k = 0;
		for (int i = 0; i < len; i++){
			if (str[i] == ')'){
				if (k != 0 && stack[k - 1] < 0)
					k--;
				else
					stack[k++] = i + 1;
			}else if (str[i] == '(')
				stack[k++] = -(i + 1);
		}
		for (int i = 0; i < k; i++){
			int index = stack[i];
			if (index < 0)	res[-index - 1] = '$';
			else			res[index - 1] = '?';
		}
		cout << str << endl;
		cout << res << endl;
	}
	return 0;
}


int main()
{
	char str[110]={'\0'};
	while(cin.getline(str,110,'\n'))
	{int length;
	length=strlen(str);
	for(int i=0;i<length;i++)
		cout<<str[i];
	cout<<endl;
	for(int i=length-1;i>=0;i--)
	{
		if(str[i]=='(')
		{
			str[i]='$';
			for(int j=i+1;j<length;j++)
			{
				if(str[j]=='?')
				{
					str[j]=' ';
					str[i]=' ';
					break;
				}
			}
		}
		else if(str[i]==')')
			str[i]='?';
		  else
			str[i]=' ';
	}
	for(int i=0;i<length;i++)
		cout<<str[i];
	cout<<endl;}
	return 0;
}


right(char s[],int i,int n)
{
	int j;
	int left=0,right=0;
	int num=0;

	for(j=i+1;j<n;j++){
		if(s[j]=='(') right++;
		if(s[j]==')') left++;
		if(right<left) {num=1;break;}
	}
	return(num);
}

left(char s[],int i)
{
	int j;
	int left=0,right=0;
	int num=0;

	for(j=i-1;j>=0;j--){
		if(s[j]=='(') right++;
		if(s[j]==')') left++;
		if(right>left) {num=1;break;}
	}
	return(num);
}

trans(char s1[],char s2[])
{
	int len;
	int i;
	len=strlen(s1);

	for(i=0;i<len;i++){
		if(s1[i]!=')'&&s1[i]!='(')
			s2[i]=' ';

			if(s1[i]==')')
			{
				if(left(s1,i)==1) s2[i]=' ';
				else s2[i]='?';
			}

			if(s1[i]=='(')
			{
				if(right(s1,i,len)==1) s2[i]=' ';
				else s2[i]='$';
			}
	}
	s2[len]='\0';
	return;
}

main()
{
	int i,j;
	int n;
	struct aaa
	{
		char s1[105];
		char s2[105];
	};
	struct aaa line[20];

	scanf("%d\n",&n);

	for(i=0;i<n;i++){

		gets(line[i].s1);
		trans(line[i].s1,line[i].s2);
	}

	for(i=0;i<n;i++)
		printf("%s\n%s\n",line[i].s1,line[i].s2);
}


int main()
{
	char ch[200],temp[200];
	int n,i,figure,tempnum[200];
	scanf("%d\n",&n);
	while(gets(ch))
	{
		figure=0;
		for(i=0;ch[i]!='\0';i++)
		{
			temp[i]=' ';
			switch(ch[i])
			{
			case '(':tempnum[figure++]=i;temp[i]='$';break;
			case ')':
				if(figure>0)
				{
					figure--;temp[tempnum[figure]]=' ';
				}
				else temp[i]='?';break;
			}
		}
		temp[i]='\0';
		printf("%s\n%s\n",ch,temp);
	}
}


int main()
{
	char line[101], mark[101];
	int locate[101], left = 0, right = 0, count = -1, len, i;
	while(cin.getline(line,101))
	{
		count = -1;
		left = 0;
		right = 0;
		len = strlen(line);
		memset(mark, ' ', sizeof(mark));
		memset(locate, 0, sizeof(locate));
		for(i = 0 ; line[i] != '\0' ; i++)
		{

			if(line[i] == '(')
			{
				left++;
				locate[++count] = i;
			}
			if(line[i] == ')')
			{
				if(left <= right)
				{
					mark[i] = '?';
				}
				else
				{
					right++;
					count--;
				}
			}
		}
		if(count >= 0)
		{
			for(i = 0 ; i <= count ; i++)
			{
				mark[locate[i]] = '$';
			}
		}
			mark[len] = '\0';
			cout << line << endl;
			cout << mark << endl;

	}
		return 0;
	}


char s[999];
int find(int i,int l)
{
	int t;
	if(s[i]=='\0') return -1;
	if(s[i]=='(')
	{
		t=find(i+1,l+1);
		if(t==-1) s[i]='$';
		else s[i]=' ';
		if(t==-1) return -1;
		else return find(t+1,l);
	}
	if(s[i]==')')
	{
		if(l==0) s[i]='?';
		else s[i]=' ';
		if(l==0) return find(i+1,0);
		else return i;
	}
	s[i]=' ';
	return find(i+1,l);
}
int main()
{
    for(;cin>>s;)
    {
    	cout<<s<<endl;
    	find(0,0);
    	cout<<s<<endl;
    }
	return 0;
}


int main() {
	char str[110] = {0};

	while(cin >> str)
	{
		int change[110] = {0};
	    char ans[110] = {0};
		int len = strlen(str);
		for(int i = 0; i < len; i++)
		{
			if(str[i] == '(')
			{
				change[i] = 1;
			}
			else if(str[i] == ')')
				change[i] = -1;
			else change[i] = 0;
		}
		int sum = 0;
		for(int i =0; i < len; i++)
		{
	          sum += change[i];
			  if(sum < 0)
			  {
				  ans[i] = '?';
				  sum = 0;
			  }

		}
		sum = 0;
		for(int i = len - 1; i >= 0; i--)
		{
	          sum += change[i];
			  if(sum > 0)
			  {
				  ans[i] = '$';
				  sum = 0;
			  }

		}
		for(int i =0; i < len; i++)
		{
			if(ans[i] != '?' && ans[i] != '$')
				ans[i] = ' ';
		}
		cout << str << endl;
		cout << ans << endl;
	}

	return 0;
}


int match(char c[],int n)
{
	for(int i=n+1;i<strlen(c);i++)
	{
		if(c[i]==')')
		{
			c[i]=' ';
			c[n]=' ';
			return i;
		}
		else if(c[i]=='(')
		{
			i=match(c,i);
		}
	}
	c[n]='$';
	return strlen(c)-1;
}
int main()
{
	char ch[110];
	while(cin>>ch)
	{
		cout<<ch<<endl;
		for(int i=0;ch[i]!=0;i++)
			if(ch[i]!='('&&ch[i]!=')')
				ch[i]=' ';
		for(int i=0;ch[i]!=0;i++)
		{
			if(ch[i]=='(')
				i=match(ch,i);
			else if(ch[i]==')')
				ch[i]='?';
		}
		cout<<ch<<endl;
	}
	return 0;
}


//
//  main.cpp
//  match
//
//  Created by blues cookie on 12-11-28.
//  Copyright (c) 2012? blues cookie. All rights reserved.
//

int length;
char a[1000];
char c[100];
int b[100];//???????????
int zkh=0;//????????
int match(int i)
{
    if(i==length) return 0;
    if(a[i]=='(')
    {
        zkh=zkh+1;
        c[i]='$';
        b[zkh]=i;
    }
    if(a[i]==')')
    {
        if(zkh==0)
        {
            c[i]='?';
        }
        if(zkh!=0)//????????
        {
            c[b[zkh]]=' ';
            zkh=zkh-1;
            c[i]=' ';
        }
    }
    if(a[i]!='(' && a[i]!=')')
    {
        c[i]=' ';
    }
    match(i+1);
}

int main(int argc, const char * argv[])
{
    int j,k,l;
    while(cin.getline(a,200))
    {
        zkh=0;
        length=strlen(a);
        for(j=0;j<length;j++)
        {
            cout<<a[j];
        }
        cout<<endl;//??????

        match(0);//??????

        for(l=0;l<length;l++)
        {
            cout<<c[l];
        }
        cout<<endl;

    }
}


char use[150],rem[150];
int len;
void rid(int begin,int end);
int main()
{while(1){
	char s;
	int i=0;
	while((s=cin.get())!='\n'){
		use[i]=s;rem[i]=s;i++;
	}
	len=i-1; //[0]->[len]
	rid(0,len);
	      for(int j=0;j<=len;j++)
		  if(use[j]=='(')
			  use[j]='$';
		  else if(use[j]==')')
			  use[j]='?';
		  else if(use[j]!='?' && use[j]!='$') use[j]=' ';

	for(i=0;i<=len;i++)
		cout<<rem[i];
	cout<<endl;
	for(i=0;i<=len;i++)
		cout<<use[i];
	cout<<endl;
}

	return 0;
}

void rid(int begin,int end)
{
  if(begin<=end){
	  if(use[begin]!=')')
		  rid(begin+1,end);
	  else {
		  int i=begin;
		  while(use[i]!='(' && i>=0)i--; //????
		    if(i==-1)
			  {use[begin]='?';rid(begin+1,end);}
			else
				for(int j=i;j<=begin;j++)
				 {use[j]=' ';rid(begin+1,end);}
	      }
   }
}


/*
???
1200015101
??????
*/
int lenth,b[200];
char a[200];
void match(int n)//???n????????
{
	for(int i=n-1;i>=0;i--)
		if(b[i]==1)
		{
			b[i]=0;
			b[n]=0;
			break;
		}
}
int main()
{
	while(cin.getline(a,105))
	{
		for(int i=0;i<strlen(a);i++)
		{
			switch(a[i])
			{
			case '(':b[i]=1;break;
			case ')':b[i]=2;break;
			default :b[i]=0;
			}
		}//??
		for(int i=0;i<strlen(a);i++)
		{
			if(b[i]==2) match(i);
		}//????
		for(int i=0;i<strlen(a);i++)
			cout<<a[i];
		cout<<endl;
		for(int i=0;i<strlen(a);i++)
			switch(b[i]){
			case 0:cout<<' ';break;
			case 1:cout<<'$';break;
			case 2:cout<<'?';break;
		}//??
		cout<<endl;
	}
	return 0;
}


int main()
{
    char a[105];
    int mark[105] = {0};
    while(cin.getline(a,101))
    {
        for (int i = 0; i < strlen(a); i++)
        {
            if (a[i] == '(')
            {
                mark[i] = 1;
            }
            if (a[i] == ')')
            {
                mark[i] = -1;
            }
        }
        for (int i = 1; i < strlen(a); i++)
        {
            if (mark[i] == -1)
            {
                for (int j = i - 1; j >= 0; j--)
                {
                    if (mark[j] == 1)
                    {
                        mark[i] = 0;
                        mark[j] = 0;
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < strlen(a); i++)
            cout << a[i];
        cout << endl;
        for (int i = 0; i < strlen(a); i++)
        {
            if (mark[i] == 0) cout << " ";
            else if (mark[i] == 1) cout << "$";
            else cout << "?";
        }
        cout << endl;
        for (int i = 0; i < strlen(a); i++)
            mark[i] = 0;
    }
    return 0;
}


char s[100];
int temp[100];
int addition[100];
int flag,i,j;
int f()
{
    for(i=0;i<strlen(s);i++)
      {
          if(s[i]=='(')  {s[i]='$';addition[flag]=i;flag++;}
          else if(s[i]==')')
            {
                if(flag)
                    {
                        s[i]=' ';
                        flag--;
                        s[addition[flag]]=' ';
                    }
                    else s[i]='?';
            }
           else s[i]=' ';
      }
}
int main()
{
    while(cin>>s)
      {
          cout<<s<<endl;
          flag=0;
          f();
          cout<<s<<endl;
      }
      return 0;
}


int main()
{
	int len;
	int i,left = 0 ,right = 0;
	char str[102],b[102];
    while (cin.getline(str,102))
	{
		len = strlen(str);
		left = 0;
		right = 0;
		for (i = 0 ; str[i] != '\0'; i++)
		{
			b[i] = ' ';
			if (str[i] == '(')
				left++;
			if (str[i] == ')')
			{
				if (left <= 0)
					b[i] = '?';
				else
					left--;
			}
		}
        for (i = len - 1 ;i >= 0; i--)
		{
			if (str[i] == ')')
				right++;
			if (str[i] == '(')
			{
				if (right <= 0)
					b[i] = '$';
				else
					right--;
			}
		}
		for (i = 0 ;i < len; i++)
			cout << str[i];
		cout << endl;
		for (i = 0 ;i < len; i++)
			cout << b[i];
		cout << endl;
	}
	return 0;
}


int main()  //????
{
	const int MAX=50000;
	int n;
	cin>>n;
	for (int k=0;k<n;k++)
	{
		int jz[110][110];
		for (int i=0;i<n;i++)
			for (int j=0;j<n;j++)
				cin>>jz[i][j];
		int ans=0;
		for (int x=0;x<n-1;x++)
		{
			for (int i=0;i<n-x;i++)
			{
				int min=MAX;
				for (int j=0;j<n-x;j++)
					if (jz[i][j]<min) min=jz[i][j];
				for (int j=0;j<n-x;j++)
					jz[i][j]-=min;
			}
			for (int i=0;i<n-x;i++)
			{
				int min=MAX;
				for (int j=0;j<n-x;j++)
					if (jz[j][i]<min) min=jz[j][i];
				for (int j=0;j<n-x;j++)
					jz[j][i]-=min;
			}
			ans+=jz[1][1];
			for (int i=0;i<n-x;i++)
				for (int j=2;j<n-x;j++)
					jz[i][j-1]=jz[i][j];
			for (int i=0;i<n-x;i++)
				for (int j=2;j<n-x;j++)
					jz[j-1][i]=jz[j][i];
		}
		cout<<ans<<endl;
	}
	return 0;
}


int main()
{
    int n,m,i,j,k,sum,min;
    cin >>m;
    for(i=1;i<=m;i++)//m×éÊý¾Ý  Ñ»·ÊäÈë
    {
        int a[m][m];
        for(j=0;j<m;j++)//Ñ»·ÊäÈë¾ØÕó
        for(k=0;k<m;k++)
        cin >>a[j][k];
        sum=0;
        for(n=m;n>1;n--)//¾ØÕó¹éÁã
    {
        for(j=0;j<n;j++)//ÐÐ¹æÁã
        {
            min=a[j][0];
            for(k=1;k<n;k++)
            {
                if(a[j][k]<min)//ÕÒ³öÃ¿ÐÐ×îÐ¡Öµ
                {
                    min=a[j][k];
                }
            }
            for(k=0;k<n;k++)
            {
                a[j][k]=a[j][k]-min;//Ã¿ÐÐ¶¼¼õÈ¥×îÐ¡Öµ
            }
        }
        for(k=0;k<n;k++)//ÁÐ¹éÁã
        {
            min=a[0][k];
            for(j=1;j<n;j++)
            {
                if(a[j][k]<min)
                {
                    min=a[j][k];
                }
            }
            for(j=0;j<n;j++)
            {
                a[j][k]=a[j][k]-min;
            }
        }
        sum=sum+a[1][1];//sum¼ÓÉÏa[1][1]
        for(j=1;j<n-1;j++)//¾ØÕóÏû¼õ
        for(k=0;k<n;k++)
        {
            a[j][k]=a[j+1][k];//ÐÐÏû¼õ
        }
        for(k=1;k<n-1;k++)//ÁÐÏû¼õ
        for(j=0;j<n;j++)
        {
            a[j][k]=a[j][k+1];
        }
    }
    cout <<sum<<endl;//Êä³ö
    }

    return 0;
}


void suo(int b[101][101],int n)
{
	int row=0,l=0;
	for(int i=1;i<n-1;i++)
		for(int j=0;j<n;j++)
			b[i][j]=b[i+1][j];
	for(int j=1;j<n-1;j++)
		for(int i=0;i<n;i++)
			b[i][j]=b[i][j+1];
return;
}

int square(int a[101][101],int n)
{
	if(n==1)
		return 0;
	for(int i=0;i<n;i++)
	{
		int min=a[i][0];
		for(int j=1;j<n;j++)
			if(min>a[i][j])
				min=a[i][j];
		for(int j=0;j<n;j++)
			a[i][j]-=min;
	}
	for(int i=0;i<n;i++)
	{
		int min=a[0][i];
		for(int j=1;j<n;j++)
			if(min>a[j][i])
				min=a[j][i];
		for(int j=0;j<n;j++)
			a[j][i]-=min;
	}
	int t=a[1][1];
	int sum=0;
	suo(a,n);
	sum=t+square(a,n-1);
	return sum;
}
int main()
{
	int i,n,k,j,m[101][101];
	cin>>n;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
			for(k=0;k<n;k++)
				cin>>m[j][k];
		cout<<square(m,n)<<endl;
	}
return 0;
}


/*
 * n2.cpp
 *
 *  Created on: 2011-11-12
 *      Author: Administrator
 */

int a[100][100];

int search1(int num,int n)      //??n???num?????
{
	int i,min;
	min=a[num][0];
	for(i=1;i<n;i++)
		if(a[num][i]<min) min=a[num][i];
	return min;
}

int search2(int num,int n)     //??n???num?????
{
	int j,min;
	min=a[0][num];
	for(j=1;j<n;j++)
		if(a[j][num]<min) min=a[j][num];
	return min;
}

void dele1(int num,int n,int x)        //?n????num??????x
{
	int i;
	for(i=0;i<=n;i++)
		a[num][i]=a[num][i]-x;
}

void dele2(int num,int n,int x)        //?n????num??????x
{
	int j;
	for(j=0;j<=n;j++)
		a[j][num]=a[j][num]-x;
}

void move(int n)                       //???2??2?
{
	int i,j;
	for(i=0;i<n;i++)
		for(j=1;j<n-1;j++)
			a[i][j]=a[i][j+1];
	for(j=0;j<n-1;j++)
		for(i=1;i<n-1;i++)
			a[i][j]=a[i+1][j];
}

int main()
{
	int n,l,i,j,m,ans;
	cin>>n;
	for(l=1;l<=n;l++){
		m=n;                        //?????
		ans=0;
		for(i=0;i<n;i++)            //????
			for(j=0;j<n;j++)
				cin>>a[i][j];

		while(m>1){                 //??????
			for(i=0;i<m;i++) dele1(i,m,search1(i,m));
			for(j=0;j<m;j++) dele2(j,m,search2(j,m));
			ans=ans+a[1][1];
			move(m);
			m=m-1;
		}

		cout<<ans<<endl;           //????
	}
	return 0;
}


void r(int m);
void l(int m);
void fun(int m);
void Try(int i);                                                       //¶¨Òå4¸öº¯Êý
int a[100][100], n, s[100] = {0};
int main()
{
	int i, j, k, sum;
	cin>>n;
	for(i = 0; i < n; i++)
	{
		sum = 0;
		for(j = 0; j < n; j++)
		{
			for(k = 0; k < n; k++)
				cin>>a[j][k];
		}
		Try(0);                                                       //±»µ÷ÓÃº¯Êý
		for(j = 0; j < n - 1; j++)
			sum = sum + s[j];                                         //sumµÄ×ÜºÍ
		cout<<sum<<endl;
	}
	return 0;
}
void r(int m)                                                         //º¯ÊýÓÃÀ´ÁÐ¹éÁã
{
	int x, y, min;
	for(x = 0; x < m; x++)
	{
		min = a[x][0];
		for(y = 0; y < m; y++)
		{
			if(a[x][y] < min)
				min = a[x][y];
		}
		for(y = 0; y < m; y++)
			a[x][y] = a[x][y] - min;
	}
}
void l(int m)                                                        //º¯ÊýÓÃÀ´ÐÐ¹éÁã
{
	int x, y, min;
	for(y = 0; y < m; y++)
	{
		min = a[0][y];
		for(x = 0; x < m; x++)
		{
			if(a[x][y] < min)
				min = a[x][y];
		}
		for(x = 0; x < m; x++)
			a[x][y] = a[x][y] - min;
	}
}
void fun(int m)                                                      //º¯ÊýÓÃÀ´Ïû¼õ
{
	int i, j;
	for(i = 0; i < m; i++)
	{
		for(j = 2; j < m; j++)
	 		a[i][j - 1] = a[i][j];
	}
	for(j = 0; j < m - 1; j++)
	{
		for(i = 2; i < m; i++)
			a[i - 1][j] = a[i][j];
	}
}
void Try(int i)
{
	r(n - i);
	l(n - i);
	s[i] = a[1][1];
	fun(n - i);
	if(i < n - 1)
		Try(i + 1);
}


int n,i,j,k,l,len=0,sum=0;
int array[100][100];
void guilinhang(int a)
{
	int min=array[a][0];
	for(l=0;l<len;l++)
	{
		if(array[a][l]<min)
			min=array[a][l];
	}
	for(l=0;l<len;l++)
		array[a][l]=array[a][l]-min;
}
void guilinlie(int a)
{
	int min=array[0][a];
	for(l=0;l<len;l++)
	{
		if(array[l][a]<min)
			min=array[l][a];
	}
	for(l=0;l<len;l++)
		array[l][a]=array[l][a]-min;
}
void xiaojian(int a)
{
	if(a==2)
		return;
	else if(a>2)
	{
		for(i=1;i<a-1;i++)
		{
			for(j=0;j<a;j++)
				array[j][i]=array[j][i+1];
		}
		for(i=1;i<a-1;i++)
		{
			for(j=0;j<a;j++)
				array[i][j]=array[i+1][j];
		}
	}
}
int main()
{
	cin>>n;
	for(k=0;k<n;k++)
	{
		len=n;
		sum=0;
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				cin>>array[i][j];
			}
		}
		while(len>=2)
		{
		    for(i=0;i<len;i++)
		    {
			    guilinhang(i);
		    }
		    for(i=0;i<len;i++)
		    {
		    	guilinlie(i);
		    }
		    sum=sum+array[1][1];
		    xiaojian(len);
		    len--;
		}
		cout<<sum<<endl;
	}
	return 0;
}


int mat[100][100], n, sum = 0;
int rmin(int a[][100], int x)
{
	int i, rsmall = a[x][0];
	for(i = 0; i < n; i++)
	{
		if(rsmall > a[x][i])
		{
			rsmall = a[x][i];
		}
	}
	return rsmall;
}
int cmin(int a[][100], int y)
{
	int i, csmall = a[0][y];
	for(i = 0; i < n; i++)
	{
		if(csmall > a[i][y])
		{
			csmall = a[i][y];
		}
	}
	return csmall;
}
void caculate(int mat[][100])
{
	int i, j, rsmall, csmall;
	for(i = 0; i < n; i++)
	{
		rsmall = rmin(mat, i);
		for(j = 0; j < n; j++)
		{
			mat[i][j] -= rsmall;
		}
	}
	for(i = 0; i < n; i++)
	{
		csmall = cmin(mat, i);
		for(j = 0; j < n; j++)
		{
			mat[j][i] -= csmall;
		}
	}
}
void change(int a[][100], int x)
{
	int i, j;
	sum += a[1][1];
	for(i = 0; i < x; i++)
		for(j = 1; j < x - 1; j++)
		{
			a[i][j] = a[i][j + 1];
		}
	for(i = 1; i < x - 1; i++)
		for(j = 0; j < x - 1; j++)
		{
			a[i][j] = a[i + 1][j];
		}
}
int main()
{
	int m, i, j, num;
	cin >> num;
	for(m = 1; m <= num; m++)
	{
		n = num;
		sum = 0;
		for(i = 0; i < num; i++)
			for(j = 0; j < num; j++)
			{
				cin >> mat[i][j];
			}
		while(n != 1)
		{
			caculate(mat);
			change(mat, n--);
		}
		cout << sum << endl;
	}cin>>i;
	return 0;
}


int main()
{
	int num;
	cin >> num;
	int n = num, numcas = num;

	int a[301][301];
	while (num--)
	{
		int s = 0;
		memset(a, 0, sizeof(a));
		for (int i = 0; i < numcas; i++)
		for (int j = 0; j < numcas; j++)
		{
			cin >> a[i][j];
		}

		n = numcas;
		while (1)
		{

			if (n == 0) {cout << s << endl; break;}

			for (int j  = 0; j < n ; j++)
			{
				int min = 1000000;
				for (int k = 0;k < n; k++)
				{
					if (a[j][k] < min) min = a[j][k];
				}
				for (int i = 0; i < n; i++) a[j][i] -= min;
			}
							/*	for (int j = 0; j < n; j++) {
			for (int i = 0; i < n; i++)
			{
				cout << a[j][i] << " ";
			}
			cout << endl;
			}*/
			for (int j  = 0; j < n ; j++)
			{
				int min = 1000000;
				for (int k = 0;k < n; k++)
				{
					if (a[k][j] < min) min = a[k][j];
				}
				for (int i = 0; i < n; i++) a[i][j] -= min;
			}

			s += a[1][1];

			for (int j = 0; j < n; j++)
			for (int i = 1; i < n; i++)
			{
				a[j][i] = a[j][i+1];
			}
			for (int j = 0; j < n - 1; j++)
			for (int i = 1; i < n; i++)
			{
				a[i][j] = a[i+1][j];
			}
			n--;
		}
	}

	return 0;
}


//
//  main.cpp
//  juzhen
//
//  Created by blues cookie on 12-11-21.
//  Copyright (c) 2012? blues cookie. All rights reserved.
//

int a[100][100];
int result=0;
int min1;
int minh(int n,int j)//???J?n?????????
{
    int i,min_a;
    min_a=a[j][0];
    for(i=0;i<n;i++)
    {
        if(min_a>a[j][i])
        {
            min_a=a[j][i];
        }
    }
    return min_a;
}
int minl(int n,int j)//????
{
    int i,min_a;
    min_a=a[0][j];
    for(i=0;i<n;i++)
    {
        if(min_a>a[i][j])
        {
            min_a=a[i][j];
        }
    }
    return min_a;
}
int dec(int n)//??
{
    int m,p,q;
    if(n==1)
    {
        cout<<result<<endl;
        return 0;
    }
    if(n>1)
    {
        for(m=0;m<n;m++)//???????
        {
            min1=minh(n,m);
           for(p=0;p<n;p++)
           {
               a[m][p]=a[m][p]-min1;
           }
        }
        for(m=0;m<n;m++)//???????
        {
            min1=minl(n,m);
            for(p=0;p<n;p++)
            {
                a[p][m]=a[p][m]-min1;

            }
        }
        result=result+a[1][1];
        for(m=0;m<n;m++)//?????
        {
            for(p=1;p<n-1;p++)
            {
              a[m][p]=a[m][p+1];
            }
         }
       for(m=0;m<n-1;m++)//?????
        {
            for(p=1;p<n-1;p++)
            {
              a[p][m]=a[p+1][m];
            }
        }
        dec(n-1);
    }
}
int main(int argc, const char * argv[])
{

    int n;
    cin>>n;
    int w,s,t;
    for(w=1;w<=n;w++)
    {
        for(s=0;s<n;s++)//????
        {

            for(t=0;t<n;t++)
            {
                cin>>a[s][t];
            }
        }
        result=0;
        dec(n);
    }
    return 0;
}


int main()
{
	int nq,times,i,j,min,m;

	cin>>nq;
	const int n=nq;
	int sum[n];
	int a[n][n][n];
	for(times=0;times<n;times++)
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				cin>>a[times][i][j];//??n?????
	for(times=0;times<n;times++)
	{sum[times]=0;
		for(m=n;m>1;m--)

		{
		for(i=0;i<m;i++)

				{
				min=a[times][i][0];
				for(j=1;j<m;j++)
				if(a[times][i][j]<min)
				min=a[times][i][j];
				for(j=0;j<m;j++)
					a[times][i][j]-=min;

				}//????
		for(j=0;j<m;j++)
		{
			min=a[times][0][j];
			for(i=1;i<m;i++)
				if(a[times][i][j]<min)
				min=a[times][i][j];
			for(i=0;i<m;i++)
				a[times][i][j]-=min;
		}//????
	sum[times]+=a[times][1][1];
		for(i=2;i<m;i++)
			for(j=0;j<n;j++)
				a[times][i-1][j]=a[times][i][j];//?????
		for(i=2;i<m;i++)
			for(j=0;j<n-1;j++)
				a[times][j][i-1]=a[times][j][i];//?????
        }
	}
for(i=0;i<n;i++)
	cout<<sum[i]<<endl;
return 0;

}


int main()
{
	int n;
	cin >> n;
	int matrix[100][100] = {0};
	int time, min, sum = 0, k;
	for (k = 1; k <= n; k++)
	{
		int i, j;
		for (i = 0; i < n; i++)
			for (j = 0; j < n; j++)
				cin >> *(*(matrix + i) + j);
		sum = 0;
		for (time = 1; time < n; time++)
		{
			for (i = 0; i <= n - time; i++)
			{
				min = *(*(matrix + i));
				for (j = 0; j <= n - time; j++)
					if (*(*(matrix + i) + j) < min) min = *(*(matrix + i) + j);
				for (j = 0; j <= n - time; j++)
					*(*(matrix + i) + j) -= min;
			}
			for (j = 0; j <= n - time; j++)
			{
				min = *(*(matrix) + j);
				for (i = 0; i <= n - time; i++)
					if (*(*(matrix + i) + j) < min) min = *(*(matrix + i) + j);
				for (i = 0; i <= n - time; i++)
					*(*(matrix + i) + j) -= min;
			}
			sum += *(*(matrix + 1) + 1);
			for (i = 1; i <= n - time; i++)
			{
				for (j = 0; j <= n - time; j++)
					*(*(matrix + i) + j) = *(*(matrix + i + 1) + j);
			}
			for (j = 1; j <= n - time; j++)
			{
				for (i = 0; i <= n - time; i++)
					*(*(matrix + i) + j) = *(*(matrix + i) + j + 1);
			}
		}
		cout << sum << endl;
	}
	return 0;
}


int	main()
{
	int	n;
	cin >> n;
	for( int c = 0 ; c < n ; c ++ )
	{
		int	mat[n][n];
		for( int i = 0 ; i < n ; i ++ )
		for( int j = 0 ; j < n ; j ++ )
			cin >> mat[i][j];

		int	s = 0;
		for( int r = n ; r > 1 ; r -- )
		{
			int	m;
			//row
			for( int i = 0 ; i < r ; i ++ )
			{
				m = mat[i][0];
				for( int j = 1 ; j < r ; j ++ )
					m = min( m , mat[i][j] );
				for( int j = 0 ; j < r ; j ++ )
					mat[i][j] -= m;
			}
			//column
			for( int j = 0 ; j < r ; j ++ )
			{
				m = mat[0][j];
				for( int i = 1 ; i < r ; i ++ )
					m = min( m , mat[i][j] );
				for( int i = 0 ; i < r ; i ++ )
					mat[i][j] -= m;
			}

			s += mat[1][1];
			for( int i = 1 ; i < r - 1 ; i ++ )
			for( int j = 0 ; j < r ; j ++ )
				mat[i][j] = mat[i + 1][j];
			for( int j = 1 ; j < r - 1 ; j ++ )
			for( int i = 0 ; i < r - 1 ; i ++ )
				mat[i][j] = mat[i][j + 1];
		}
		cout << s << endl;
	}

	return 0;
}


int main()
{
	int n,i,j,k,len,sum;	//len : the length of matrix
	int m[100][100];
	void cutr0(int m[100][100],int len);
	void cutc0(int m[100][100],int len);
	void del(int m[100][100],int n);//delete row[1] and column[1];return n-1;
	cin >> n;
	for (k=1;k<=n;k++)
	{
		sum=0;
		len=n;
		for (i=0;i<n;i++)
			for (j=0;j<n;j++)
				cin >> m[i][j];

		while (len>1)
		{
			cutr0(m,len);
			cutc0(m,len);
			sum +=m[1][1];
			del(m,len);
			len--;
		}
		cout << sum << endl;
	}

	return 0;
}

void cutr0(int m[100][100],int len)
{
	int min[100];		//min[i] stores the min value of row[i]
	int i,j;

	for (i=0;i<len;i++)
	{
		min[i]=m[i][0];
		for (j=1;j<len;j++)
			if (min[i]>m[i][j])
				min[i]=m[i][j];
	}

	for (i=0;i<len;i++)
		for (j=0;j<len;j++)
			m[i][j] -= min[i];
}

void cutc0(int m[100][100],int len)
{
	int min[100];		//min[i] stores the min value of column[i]
	int i,j;

	for (i=0;i<len;i++)
	{
		min[i]=m[0][i];
		for (j=1;j<len;j++)
			if (min[i]>m[j][i])
				min[i]=m[j][i];
	}

	for (i=0;i<len;i++)
		for (j=0;j<len;j++)
			m[i][j] -= min[j];
}

void del(int m[100][100],int len)
{
	int i,j;
	for (i=2;i<len;i++)		//deal with row[0] and column[0]
	{
		m[0][i-1]=m[0][i];
		m[i-1][0]=m[i][0];
	}
	for (i=2;i<len;i++)
		for (j=2;j<len;j++)
			m[i-1][j-1]=m[i][j];
}


int min2;
  int min1;
void diguihang(int i,int n,int a[300][300])//to work out the min of every line
{   if(i==-1)return;
	min1=a[i][0];//min1 = INT_MAX;
	for(int j=0;j<n;j++)
	{
		if(a[i][j]<min1)min1=a[i][j];
	}

		for(int v=0;v<n;v++)
		{
			a[i][v]=a[i][v]-min1;

		}
		diguihang(i-1,n,a);//a???????

}

void diguilie(int i,int n,int a[300][300])//the same as above to work out the min of
{
	if(i==-1)return;
	min2=a[0][i];//min2 = INT_MAX;
	for(int j=0;j<n;j++)
	{

		if(a[j][i]<min2)min2=a[j][i];
	}

		for(int v=0;v<n;v++)
		{
			a[v][i]=a[v][i]-min2;

		}
		diguilie(i-1,n,a);
	}

int change(int n,int sum,int a[300][300])//to shorten
{
	if (n==1)return sum;

		diguihang(n-1,n,a);
		diguilie(n-1,n,a);
		sum=sum+a[1][1];
	for(int j=1;j<n-1;j++)
	for(int i=0;i<n;i++)
	{
		a[j][i]=a[j+1][i];
	}
	for(int j=1;j<n-1;j++)
		for(int i=0;i<n-1;i++)
		{
			a[i][j]=a[i][j+1];
		}

      return  change(n-1,sum,a);
}
int main()
{
	int N;
	int a[300][300];
	cin>>N;
	for(int i=1;i<=N;i++){
		for(int j=0;j<N;j++)
			for(int y=0;y<N;y++)
			{
				cin>>a[j][y];
			}
		cout<<change(N,0,a)<<endl;
	}
	return 0;
}


/*
 * matrix_sub.cpp
 *
 *  Created on: 2012-12-28
 *      Author: 12001
 */

int main() {
	int n;
	int matrix[100][100];

	cin >> n;
	for (int i = 0; i < n; i++) {
		int sum = 0;
		for (int j = 0; j < n; j++)
			for (int k = 0; k < n; k++)
				cin >> matrix[j][k];
		for (int m = n; m > 1; m--) {
			// for line
			for (int j = 0; j < m; j++) {
				int min = matrix[j][0];
				for (int k = 1; k < m; k++) {
					if (matrix[j][k] < min)
						min = matrix[j][k];
				}
				for (int k = 0; k < m; k++)
					matrix[j][k] -= min;
			}
			// for column
			for (int j=0; j<m; j++){
				int min = matrix[0][j];
				for (int k = 1; k < m; k++) {
					if (matrix[k][j] < min)
						min = matrix[k][j];
				}
				for (int k=0;k<m;k++)
					matrix[k][j]-=min;
			}

			sum += matrix[1][1];
			for (int i = 1; i < m; i++){
				for (int j = 1; j < m; j++)
					matrix[i][j] = matrix[i + 1][j + 1];
				matrix[0][i]=matrix[0][i+1];
				matrix[i][0]=matrix[i+1][0];
			}
		}
		cout << sum << endl;
	}
	return 0;
}


 /* homework2.cpp
 *
 *  Created on: 2012-11-10
 *      Author: Lixurong
 */

int a[101][101], sum=0;

void zero( int n )
{
	for( int i=0; i<n; i++ )
	{
		int min;
		min=a[i][0];
		for( int j=0; j<n; j++ )
		{
			if( a[i][j]<min )
			{
				min = a[i][j];
			}
		}
		for( int j=0; j<n; j++ )
		{
			a[i][j]-=min;
		}
	}
	for( int j=0; j<n; j++ )
	{
		int min2;
		min2=a[0][j];
		for( int i=0; i<n; i++ )
		{
			if( a[i][j]<min2 )
			{
				min2 = a[i][j];
			}
		}
		for( int i=0; i<n; i++ )
		{
			a[i][j]-=min2;
		}
	}
}

void subtract( int n )
{
	if( n>2 )
	{
		for( int j=2; j<n; j++ )
		{
			a[0][j-1]=a[0][j];
		}
		for( int i=2; i<n; i++ )
		{
			a[i-1][0]=a[i][0];
		}
		for( int i=2; i<n; i++ )
		{
			for( int j=2; j<n; j++ )
			{
				a[i-1][j-1]=a[i][j];
			}
		}
	}
}

void process( int n )
{
	if( n!=1 )
	{
	    zero(n);
	    sum += a[1][1];
	    subtract(n);
	    process(n-1);
	}
}
int main()
{
	int n;
	cin >> n;
	for( int p=0; p<n; p++ )
	{
		for( int i=0; i<n; i++ )
		{
			for( int j=0; j<n; j++ )
			{
				cin >> a[i][j];
			}
		}
		process(n);
		cout << sum;
		if( p!=n-1 )
			cout << endl;
		sum=0;
	}

}


int a[111][111];
int n;

int xiao(int size);

int sum()
{
    int temp=0;
    for(int siz=n;siz>=2;siz--)
    {
        temp+=xiao(siz);
    }
    return temp;
}

int xiao(int size)
{
    for(int i=1;i<=size;i++)
    {
        int tempmin=100000;
        for(int j=1;j<=size;j++)
            if(a[i][j]<tempmin)
            tempmin=a[i][j];
        for(int j=1;j<=size;j++)
            a[i][j]-=tempmin;
    }
    for(int i=1;i<=size;i++)
    {
        int tempmin=100000;
        for(int j=1;j<=size;j++)
            if(a[j][i]<tempmin)
            tempmin=a[j][i];
        for(int j=1;j<=size;j++)
            a[j][i]-=tempmin;
    }

    int daan=a[2][2];

    for(int i=1;i<=n;i++)
        for(int j=2;j<n;j++)
            a[i][j]=a[i][j+1];
    for(int i=1;i<n;i++)
        for(int j=2;j<n;j++)
            a[j][i]=a[j+1][i];
   return daan;

}

int main()
{
    cin>>n;
    for(int t=1;t<=n;t++)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                cin>>a[i][j];
        cout<<sum()<<endl;
    }

    return 0;
}


int a[100][100];
int total;

void slice(int);
void zero(int n)
{
    for (int i = 0; i < n; i++) {
        int min = a[i][0];
        for (int j = 0; j < n; j++) {
            if (a[i][j] < min) {
                min = a[i][j];
            }
        }
        for (int j = 0; j < n; j++) {
            a[i][j] -= min;
        }
    }
    for (int i = 0; i < n; i++) {
        int min = a[0][i];
        for (int j = 0; j < n; j++) {
            if (a[j][i] < min) {
                min = a[j][i];
            }
        }
        for (int j = 0; j < n; j++) {
            a[j][i] -= min;
        }
    }
    slice(n);
}

void slice(int n)
{
    total += a[1][1];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (j > 0 && i > 0) {
                a[i][j] = a[i+1][j+1];
            } else if (j > 0) {
                a[i][j] = a[i][j+1];
            } else if (i > 0) {
                a[i][j] = a[i+1][j];
            }
        }
    }
    n--;
    if (n == 1) {
        return;
    }
    zero(n);
}

int main(int argc, const char * argv[])
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        memset(a, 0, 100*100*sizeof(int));
        total = 0;
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                cin >> a[j][k];
            }
        }
        zero(n);
        cout << total << endl;
    }
}


int a[200][200];
int minhang(int hang,int geshu)
{
    int i;

    int min=*(*(a+hang)+1);
    for(i=1;i<=geshu;i++)
    {
        if(*(*(a+hang)+i)<min) min=*(*(a+hang)+i);
    }
    return min;
}
int minlie(int lie,int geshu)
{
    int i;
    int min=*(*(a+1)+lie);
    for(i=1;i<=geshu;i++)
    {
        if(*(*(a+i)+lie)<min) min=*(*(a+i)+lie);
    }
    return min;
}
int main()
{

    int n,x,i,j;
    cin>>n;

    for(x=1;x<=n;x++)
    {

        memset(a,0,sizeof a);

        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++) cin>>*(*(a+i)+j);

        }
        int sum=0;
        for(int m=0;m<n-1;m++)
        {
            for(int mm=1;mm<=n-m;mm++)
            {
                int Minhang=minhang(mm,n-m);
                for(i=1;i<=n-m;i++)
                *(*(a+mm)+i)=*(*(a+mm)+i)-Minhang;

            }
            for(int mm=1;mm<=n-m;mm++)
            {
                int Minlie=minlie(mm,n-m);
                for(i=1;i<=n-m;i++)
                *(*(a+i)+mm)=*(*(a+i)+mm)-Minlie;

            }

            sum+=*(*(a+2)+2);
            for(int ii=1;ii<=n-m;ii++)
            {
                for(i=3;i<=n-m;i++)
                *(*(a+ii)+i-1)=*(*(a+ii)+i);

            }
            for(int ii=1;ii<=n-m;ii++)
            {
                for(i=3;i<=n-m;i++)
                *(*(a+i-1)+ii)=*(*(a+i)+ii);

            }

        }
        cout<<sum<<endl;

    }

    return 0;

}


//????HW8_02.cxx
//?????(1100019007)
//???2012-11-18
//????????????

int arr[100][100];
void solve(int n);//??
void arrayin(int n);//????
int down_cast(int n);//??????
void matrix_out(int n);//????
int main(int argc, char **argv)
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)solve(n);
	return 0;
}
void arrayin(int n){
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>arr[i][j];
		}
	}
}
void solve(int n){
	arrayin(n);
	cout<<down_cast(n)<<endl;
}
int down_cast(int n){
	if(n==1)return 0;
	int min,res;
	for(int i=0;i<n;i++){
		min=65535;
		for(int j=0;j<n;j++)if(arr[i][j]<min)min=arr[i][j];
		for(int j=0;j<n;j++)arr[i][j]-=min;
	}//???
	for(int i=0;i<n;i++){
		min=65535;
		for(int j=0;j<n;j++)if(arr[j][i]<min)min=arr[j][i];
		for(int j=0;j<n;j++)arr[j][i]-=min;
	}//???
	res=arr[1][1];
	for(int i=2;i<n;i++)for(int j=0;j<n;j++)arr[i-1][j]=arr[i][j];
	for(int i=2;i<n;i++)for(int j=0;j<n;j++)arr[j][i-1]=arr[j][i];
	//matrix_out(n);
	return res+down_cast(n-1);//????
}
void matrix_out(int n){
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cout<<arr[i][j]<<" ";
		}
		cout<<endl;
	}
}


int sm1(int line,int sum1,int array1[100][100])
{
	int min=array1[line][0];
	for (int i=0;i<sum1;i++)
		if (min>array1[line][i])
			min=array1[line][i];
	return min;
}
int sm2(int row,int sum2,int array2[100][100])
{
	int min=array2[0][row];
	for (int i=0;i<sum2;i++)
		if (min>array2[i][row])
			min=array2[i][row];
	return min;
}
int main ()
{
	int n;
	cin>>n;
	for (int step=0;step<n;step++)
	{
		int jz[100][100],he=0;
		 for (int i=0;i<n;i++)
		     for (int j=0;j<n;j++)
			   cin>>jz[i][j];
		   for (int Count=0;Count<n-1;Count++)
		   {
			   for (int i=0;i<n-Count;i++)
			   {   int minl=sm1(i,n-Count,jz);
			      for (int j=0;j<n-Count;j++)
			       jz[i][j]-=minl;
			   }
		      for (int j=0;j<n-Count;j++)
		      {    int minr=sm2(j,n-Count,jz);
			      for (int i=0;i<n-Count;i++)
			       jz[i][j]-=minr;
		       }
		       he+=jz[1][1];
		       for (int i=2;i<n-Count;i++)
		    	   for (int j=2;j<n-Count;j++)
		    		   jz[i-1][j-1]=jz[i][j];
		       for (int i=2;i<n-Count;i++)
		    	   jz[i-1][0]=jz[i][0];
		       for (int j=2;j<n-Count;j++)
		    	   jz[0][j-1]=jz[0][j];
		   }
		   cout<<he<<endl<<endl;
    }
	return 0;
}