#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* node=(node*)malloc(sizeof(node));
    strcpy(node->name,name);
    node->quantity=quantity;
    node->left=node->right=NULL;
    return node;
}

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)!=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;
}