The Librarian's Lost Alphabet

medium graphs topological-sort queue strings 1000 ms · 256 MB

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.

Any correct answer is accepted.

Example 1

Input

5
wrt
wrf
er
ett
rftt

Output

wertf

Example 2

Input

2
abc
ab

Output

IMPOSSIBLE

Hints · reveal one at a time if you're stuck

Hint 1

Only neighbouring titles matter. Where two neighbours first differ, the earlier title's letter must come before the later title's letter. Think about what a neighbouring pair where one title is a prefix of the other requires, and when that is already impossible.

Hint 2

Collect these letter-before-letter relations as a directed graph over the letters that appear. An alphabet exists exactly when this graph has no cycle, and any topological ordering of the letters in the graph gives one. Make sure your output contains only the letters that occur in the input.

report an issue Prefer your own editor? sooks prep 92 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.