The Librarian's Lost Alphabet
The reading room of the old Vastori Library holds one long shelf of catalogue cards. Each card names a single book using only lowercase letters, and the shelf holds the cards in a fixed left-to-right order that nobody has disturbed in decades.
The librarian who filed the cards used some private order of the lowercase letters — not necessarily the one you know — and filed every card the way a dictionary files words, just under her own letter order:
- Comparing two neighbouring cards letter by letter from the start, the first position where they differ must show the earlier card's letter coming before the later card's letter in her alphabet.
- If two neighbouring cards never differ up to the end of the shorter title (that is, one title is exactly a prefix of the other), the shorter title must be the earlier of the two. No alphabet can place a book after a longer title that merely continues it.
You are given the n titles in shelf order. Recover an alphabet — an order
of the letters that actually occur among the titles — that is consistent
with every neighbouring pair on the shelf, or determine that no such
alphabet could have produced this shelf.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number
of catalogue cards.
Each of the next n lines contains one title: a nonempty string of
lowercase English letters. The total length of all n titles is at most
10^5.
Output
If no ordering of the letters explains the shelf, print a single line:
IMPOSSIBLE
Otherwise print a single line containing every letter that occurs in at least one title, each exactly once, arranged in an order consistent with the shelf (a letter printed earlier comes earlier in the librarian's alphabet). If several orders work, print any one of them.
Notes
In the first example, comparing neighbouring titles gives: t before f
(from "wrt" / "wrf"), w before e (from "wrf" / "er"), r before t
(from "er" / "ett"), and e before r (from "ett" / "rftt"). The order
wertf satisfies every one of these relations; it happens to be the only
order of these five letters that does.
In the second example, "ab" is a prefix of "abc", but "abc" sits on the
shelf before "ab" — exactly the one situation no alphabet can produce, so
the answer is IMPOSSIBLE.