// editor4
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node{
    char name[50];
    int quantity;
    struct node *left;
    struct node *right;
}Node;
  Node* createnode(char name[],int quantity){
      Node* node=(Node*)malloc(sizeof(Node));
      strcpy(node->name, name);
      node->quantity=quantity;
      node->left=node->right=NULL;
  }  
 Node* insert(Node* root char name[],int quantity){
     if(root==NULL)
       return createnode(name,quantity);
     if(quantity<root->quantity)
       root->left=insert(root->left,name,quantity);
     else
       root->right=insert(root->right,name,quantity);
     return  root;
 }
  void levelOrder(Node* root){
      if(root==NULL) return;
      
      Node* queue[100];
      int front=0;rear=0;
      queue[rear++]=root;
      while(front<rear){
          Node* current=queue[front++];
          printf("%s(%d)\n",current->name,current->quantity);
          if(current->left) queue[rear++]=current->left;
          if(current->right) queue[rear++]=current->right;
      }
  }
  int main(){
      int n;
      if(scanf("%d\n"&n)!=1 || n<0 || n>10){
          printf("Invalid input");
          return 0;
      }
      Node* root=NULL;
      char name[50];
      int quantity;
      for(int i=0;i<n;i++){
          if(scanf(" %49[^,),%d",name,&quantity!=2||quantity<0){
              printf("Invalid input");
              return 0;
          }
          root=insert(root,name,quantity);
      }
      levelOrder(root);
      return 0;
  }