/******************************************************************************
* Sample solution for the Functional problem in ProgSM'02
* Author: Andreas Björklund
******************************************************************************/
#include<stdio.h>
#include<string.h>

char buf[2][1000000];

int who;
int posit;
int cnt;
char rep;
int argustart,argustop;

void parseReplace(int bound)
{
	int k;
	
	switch (buf[who][posit])
	{
		case '(':
			buf[1-who][cnt++]=buf[who][posit++];
			parseReplace(bound);
			buf[1-who][cnt++]=buf[who][posit++];
			parseReplace(bound);
			break;
		case 'L':
			buf[1-who][cnt++]=buf[who][posit++];
			if (buf[who][posit]==rep)	bound = 1;
			buf[1-who][cnt++]=buf[who][posit++];
			buf[1-who][cnt++]=buf[who][posit++];
			parseReplace(bound);
		  break;
		default:
			//printf("c:%c\n",buf[who][posit]);
			if (buf[who][posit]==rep)
			{
				if (bound) buf[1-who][cnt++]=buf[who][posit++]; else
				{	
					//Copy argument
					for (k=argustart;k<=argustop;k++)
          {
            buf[1-who][cnt++]=buf[who][k];
          }
          posit++;
				}
			} else buf[1-who][cnt++]=buf[who][posit++];
			break;
	};
}

int main()
{  
  int i,j,k,count,pardep,runner,searchArgbeg,searchArgend,deepest,where;
  
  gets(buf[0]);
  while (strcmp("z",buf[0])!=0)
  {
    who=0;count=0;
    do
    {
      deepest=0;
      searchArgbeg=searchArgend=0;
      pardep=0;
      runner=0;
      while (buf[who][runner])
      {
        switch (buf[who][runner])
        {
          case '(':
            pardep++;
            break;
          case ')':
            pardep--;
            if (searchArgbeg && pardep+1==deepest)
            {
              argustart=runner+1;
              searchArgbeg=0;
              searchArgend=1;
            }
            if (searchArgend && pardep+2==deepest)
            {
              argustop=runner-1;
              searchArgend=0;
            }
            break;
          case 'L':
            if (runner && buf[who][runner-1]=='(')
            if (pardep>deepest)
            {
              deepest=pardep;
              where=runner;
              searchArgbeg=1;
              searchArgend=0;
            }
        }
        runner++;
      }
      if (searchArgend) argustop=runner-1;
      
      // Copy to other string
      if (deepest>0)
      {
        cnt=0;
        for (i=0;i<where-1;i++) buf[1-who][cnt++]=buf[who][i];
        posit=where+3;
        rep = buf[who][where+1];
        parseReplace(0);
        for (i=argustop+1;i<runner;i++) buf[1-who][cnt++]=buf[who][i];
        buf[1-who][cnt++]=0;
        who=1-who;
        count++;
        //printf("%s\n",buf[who],deepest);
      }
    }
    while (deepest && count<=1000);
    if (count>1000) printf("unterminated\n"); else printf("%s\n",buf[who]);
    gets(buf[0]);  
  }
  printf("z\n");

  return 0;
}
