Popularity List

Interview

Time limit1sMemory limit128 MB

Summary
Reconstruct the lexicographically smallest previous week's ranking consistent with UP/DOWN/SAME movement marks of this week's list.
Level

Medium5 of 10

Topics
Greedy, Array, Simulation
Solved
No attempts yet

Statement

Every Sunday afternoon a TV music show presents the same set of songs and, based on viewer votes, shows a popularity list (ranking) of those songs. Position 1 is the top of the list.

This week's list shows, for every position, the name of the song currently there together with a mark describing how that song moved compared with last week's list:

  • UP — the song climbed to a better (smaller-numbered) position than it had last week.
  • DOWN — the song fell to a worse (larger-numbered) position than it had last week.
  • SAME — the song is at exactly the same position as last week.

You missed last week's list. Using this week's list and the movement marks, reconstruct a last week's list that is consistent with them.

In other words, a song marked UP at this week's position i occupied some last-week position greater than i; a song marked DOWN occupied some last-week position smaller than i; and a song marked SAME occupied position i last week too.

Input

The first line contains an integer N (1 ≤ N ≤ 100), the number of songs on the list.

The next N blocks describe this week's list from position 1 to position N. Each block consists of two lines:

  • the name of the song at that position — at most 100 uppercase English letters (A–Z);
  • one of the words UP, DOWN, or SAME, describing how that song moved relative to last week.

For the given input at least one valid last-week list always exists. Song names are distinct.

Output

Output last week's list as N lines: the i-th line contains the name of the song that occupied last-week position i.

Several last-week lists may be consistent with the input. Output the lexicographically smallest one: compare two candidate lists line by line from the top, and prefer the list whose first differing line holds the alphabetically smaller song name.

Examples5

  1. Example 1

    Input
    4
    LOVE
    UP
    WINE
    UP
    WATER
    DOWN
    HATE
    DOWN
    
    Expected output
    HATE
    WATER
    LOVE
    WINE
    
  2. Example 2

    Input
    4
    HOTDOG
    UP
    HAMBURGER
    DOWN
    ICECREAM
    SAME
    FRENCHFRIES
    DOWN
    
    Expected output
    HAMBURGER
    FRENCHFRIES
    ICECREAM
    HOTDOG
    
  3. Example 3

    Input
    5
    HIGHHOPES
    UP
    LOWFEELINGS
    UP
    UPANDDOWN
    DOWN
    IAMSTILLSTANDING
    DOWN
    FOOLINGAROUND
    DOWN
    
    Expected output
    FOOLINGAROUND
    UPANDDOWN
    IAMSTILLSTANDING
    HIGHHOPES
    LOWFEELINGS
    
  4. Example 4

    Input
    1
    SOLO
    SAME
    
    Expected output
    SOLO
    
  5. Example 5

    Input
    2
    BRAVO
    UP
    ALPHA
    DOWN
    
    Expected output
    ALPHA
    BRAVO