14804 - Simulated bookshelf management system   

Description

Write a program to simulate a bookshelf system.
Assume you have 255 books and a bookshelf that can hold up to 8 books.
Each book is numbered from 1 to 255, and initially, all books are on the desk (i.e., the bookshelf is empty).

When you want to read a book:

  • If the book is already on the bookshelf, take it out and read it, then place it on the rightmost side of the bookshelf.

  • If the book is not on the bookshelf, take it from the desk and also place it on the rightmost side of the bookshelf.

If the bookshelf is already full (8 books) when adding a new one, the leftmost book (the one that has stayed the longest) is removed from the bookshelf, and the new book is placed at the rightmost position.


Your task is to simulate this process and output the final state of the bookshelf after a sequence of reading operations.

Input

A sequence of integers between 1 and 255, representing the order in which books are read.
The program should process all inputs until EOF.

Ex.1 2 3 4 5 6 7 8 6 10 23 7 4

Output

8 integers, representing the final state of the bookshelf from left to right.
If a position on the bookshelf is empty, output 0 for that position.

Ex. 3 5 8 6 10 23 7 4

Sample Input  Download

Sample Output  Download




Discuss