#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
    int sessionID;
    int duration;
    struct Node*prev;
    struct Node*next;
}Node;
Node*createNode(int id,int dur){
    Node*newNode=(Node*)malloc(sizeof(Node));
    if(!newNode){
        printf("Memory allocation failed\n");
        exit(1);
    }
    newNode->sessionID=id;
    newNode->duration=dur;
    newNode->prev=newNode->next=NULL;
    return newNode;
}
void appendNode(Node**head,Node**tail,int id,int dur){
    Node*newNode=createNode(id,dur);
    if(*head==NULL){
        *head=*tail=newNode;
    }else{
        (*tail)->next=newNode;
        newNode->prev=*tail;
        *tail=newNode;
    }
}
void printList(Node*head){
    if(!head){
        printf("Log is empty");
        return;
    }
    Node*temp=head;
    while(temp){
        printf("%d%d\n",temp->sessionID,temp->duration);
        temp=temp->next;
    }
}
void freeList(Node*head){
    Node*temp;
    while(head){
        temp=head;
        head=head->next;
        free(temp);
    }
}
int main(){
    int n;
    if(scanf("%d",&n)!=1||n<0){
        printf("Invalid input");
        return 0;
    }
    Node*head=NULL;
    Node*tail=NULL;
    for(int i=0;i<n;i++){
        int id,dur;
        if(scanf("%d%d",&id,&dur)!=2){
            printf("Invalid input");
            freeList(head);
            return 0;
        }
        appendNode(&head,&tail,id,dur);
    }
    if(!tail){
        printf("Log is empty");
        return 0;
    }
    printf("%d%d\n",tail->sessionID,tail-<duration);
    if(tail->prev){
        tail=tail->prev;
        free(tail->next);
        tail->next=NULL;
    }else{
        free(tail);
        head=tail=NULL;
    }
    printfList(head);
    freeList(head);
    return 0;
}