SDE Path

Longest Word with All Prefixes Present

Medium

Longest Word with All Prefixes Present

Find the longest word in a list such that every prefix of the word also exists in the list.

Input format

The first line contains N. The next N lines contain distinct words (lowercase, ≤8 chars).

Output format

Print the longest word whose every prefix is in the list; ties → lexicographically smallest; none → -1.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
5
a aa aaa aaaa aaaaa
Expected output
aaaaa
Example 2
Input
2
a aa
Expected output
aa