Under construction

Still being built. Things may change

Word Game

Interview

Time limit1sMemory limit1024 MB

Summary
Decide whether all dictionary words can form one chain where each word starts with the previous word's last character, and print an order if so.
Level

Medium4 of 10

Topics
Graph, DFS
Solved
No attempts yet

Problem

Children play a word chain game. Each player must say a word that begins with the same character that the previous word ends with.

You are given a dictionary of words. Determine whether the children can play so that every word in the dictionary is used exactly once. All words in the dictionary are distinct. Words consist of the characters A to Z, a to z, and 0 to 9, and uppercase and lowercase letters are different characters.

Input

The first line contains the number of words NN in the dictionary (2≤N≤10 0002 \le N \le 10\,000). Each of the next NN lines contains one word. No word is longer than 10 characters.

Output

If all words can be ordered according to the rules, print JAH on the first line, followed by exactly NN lines that give one possible order of the words, where each word begins with the same character that the previous word ended with. Otherwise, print EI on a single line.

Examples2

  1. Example 1

    Input
    7
    Kala
    aabits
    aju2
    234
    SALK
    sada
    anarhia
    
    Expected output
    JAH
    SALK
    Kala
    anarhia
    aabits
    sada
    aju2
    234
    
  2. Example 2

    Input
    3
    Abbb
    Baaa
    aaaB
    
    Expected output
    EI