#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct treenode{
    char name[50];
    int quantity;
    struct treenode*left;
    struct treenode*right;
}node;
node*createnode(char name[],int quantity){
    node* newnode=(node*)malloc(sizeof(node));
    strcpy(newnode->name,name);
    newnode->quantity=quantity;
    newnode->left=newnode->right=NULL;
    return newnode;
}
node*insert(node*root,char name[],int quantity){
    if(root==NULL)
    return createnode(name,quantity);
    if(quantity<root->quantity)
    root->left=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++]=currnt->right;
    }
}
int main(){
    int n;
    if(scanf("%d",&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||quantity>1000){
    printf("invalid input");
    return 0;
    }
    root=insert(root,name,quantity);
}
levelorder(root);
return 0;
}