This page is still under construction.

Parts of this page are still being built. What you see may change.

Recently Used Documents

Interview

Time limit1sMemory limit512 MB

Summary
Simulate a most-recently-used list of capacity k: each opened document moves to the front, new ones are inserted there and the back is dropped when full, then print the final list.
Level

Easy2 of 10

Topics
Simulation, Implementation, Array, Linked list
Solved
No attempts yet

Problem

Many programs keep a list of recently used documents, called the NKD list. As the name says, the list holds the documents the user opened most recently, so the user can reopen one without searching through every document. The list has a capacity, which is the largest number of documents it can hold at one time.

Every time the user opens a document, one of the following two things happens. The rule is the same whether the user picks the document from the list or opens it some other way.

  1. If the document is already somewhere in the list, it moves to the front of the list.
  2. Otherwise it is inserted at the front of the list. If that puts the list over its capacity, the last document in the list is thrown out.

The list is empty at the start. Given the capacity of the list and the order in which the user opens documents, determine the contents of the list after all of them have been opened.

Input

The first line contains the capacity of the list kk (1≤k≤101 \le k \le 10).

The second line contains the number of documents the user opens, nn (1≤n≤5001 \le n \le 500).

Each of the next nn lines contains the name of one document the user opens, given in the order the user opens them. Every name is a string of at most 10 lowercase English letters and contains no spaces.

Output

Print the contents of the list after all of the documents have been opened, one document per line. The document printed on the first line is the one at the front of the list.

Examples2

  1. Example 1

    Input
    4
    3
    a
    b
    c
    
    Expected output
    c
    b
    a
    
  2. Example 2

    Input
    2
    6
    buba
    koko
    buba
    ivan
    ivan
    koko
    
    Expected output
    koko
    ivan