#include <fstream>
#include <string>

using namespace std;

char stack[256];
int tos(0);

ifstream in("DATA4.txt");
ofstream out("OUT4.txt");

int checkPop()
{
   return stack[tos-1];
       
}

void check(string lexon)
{
     for (int i(0); i < lexon.length(); i++)
         {
             if (lexon[i] == '{' || lexon[i] == '[' || lexon[i] == '(' )
             {
                stack[tos] = lexon[i]; 
                tos++;            
             } else if ( lexon[i] == '}' || lexon[i] == ']' || lexon[i] == ')' ) {
                
                
                char pop = checkPop();
                if (lexon[i] == '}' && pop == '{') {
                   stack[tos] = 0;
                   tos--;             
                } else if (lexon[i] == ']' && pop == '[') {
                   stack[tos] = 0;
                   tos--;             
                }  else if (lexon[i] == ')' && pop == '(') {
                   stack[tos] = 0;
                   tos--;             
                } else {
                      out<<"not balanced\n";
                      return;       
                }
                   
             }
         }//end for
         out<<"balanced\n";
              
}

int main(){

    for( int j(0); j < 5; j++)
    {
         string lexon;
         in>>lexon;
         check(lexon);
         
         for (; tos > -1; tos--)
         {
             stack[tos] = 0;    
         }
         
    }
        
}

