#include <stdio.h>
#include <stdlib.h>
struct Node 
{
    int sessionID;
    i#include <stdio.h>
#include <stdlib.h>
struct Node 
{
    int sessionID;
    int duration;
    struct Node *next;
    struct Node *prev;
};
struct Node* newNode(int id, int dur) 
{
    struct Node* node = (struct Node*) malloc(sizeof(struct Node));
    node->sessionID = id;
    node->duration = dur;
    node->next = NULL;
    node->prev = NULL;
    return node;
}
int main() 
{
    int n;
    if (scanf("%d", &n) != 1) 
    {
        printf("Invalid input");
        return 0;
    }
    struct Node *head = NULL, *tail = NULL;
    for (int i = 0; i < n; i++) 
    {
        int id, dur;
        if (scanf("%d %d", &id, &dur) != 2) 
        {
            printf("Invalid input");
            return 0;
        }
        struct Node *node = newNode(id, dur);
        if (head == NULL) 
        {
            head = tail = node;
        } 
        else 
        {
            tail->next = node;
            node->prev = tail;
            tail = node;
        }
    }
    if (tail == NULL) 
    {
        printf("Log is empty");
        return 0;
    }
    printf("%d %d\n", tail->sessionID, tail->duration);  
    tail = tail->prev;
    if (tail != NULL) 
    {
        tail->next = NULL;
    } 
    else
    {
        head = NULL; 
    }
    if (head == NULL) 
    {
        printf("Log is empty");
    } 
    else 
    {
        struct Node *current = head;
        while (current != NULL) 
        {
            printf("%d %d\n", current->sessionID, current->duration);
            current = current->next;
        }
    }
    return 0;
}nt duration;
    struct Node *next;
    struct Node *prev;
};
struct Node* newNode(int id, int dur) 
{
    struct Node* node = (struct Node*) malloc(sizeof(struct Node));
    node->sessionID = id;
    node->duration = dur;
    node->next = NULL;
    node->prev = NULL;
    return node;
}
int main() 
{
    int n;
    if (scanf("%d", &n) != 1) 
    {
        printf("Invalid input");
        return 0;
    }
    struct Node *head = NULL, *tail = NULL;
    for (int i = 0; i < n; i++) 
    {
        int id, dur;
        if (scanf("%d %d", &id, &dur) != 2) 
        {
            printf("Invalid input");
            return 0;
        }
        struct Node *node = newNode(id, dur);
        if (head == NULL) 
        {
            head = tail = node;
        } 
        else 
        {
            tail->next = node;
            node->prev = tail;
            tail = node;
        }
    }
    if (tail == NULL) 
    {
        printf("Log is empty");
        return 0;
    }
    printf("%d %d\n", tail->sessionID, tail->duration);  
    tail = tail->prev;
    if (tail != NULL) 
    {
        tail->next = NULL;
    } 
    else
    {
        head = NULL; 
    }
    if (head == NULL) 
    {
        printf("Log is empty");
    } 
    else 
    {
        struct Node *current = head;
        while (current != NULL) 
        {
            printf("%d %d\n", current->sessionID, current->duration);
            current = current->next;
        }
    }
    return 0;
}