#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct node{
    char name[100];
    int quantity;
    struct node *left;
    struct node *right;
}Node;
Node *create(char name[],int quantity){
    Node newnode=(Node)malloc(sizeof(Node));
    strcpy(newnode->name,name);
    newnode->quantity=quantity;
    newnode->left=NULL;
    newnode->right=NULL;
    return newnode;
}
Node  *insert(Node *root,char name[],int quantity){
    if(root==NULL)
        return create(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;
}