For each song, find the shortest substring that appears in that song and in no other song, breaking ties by a custom lexicographic order.
Medium4String matchingBrute forceNo attempts yetTime limit5sMemory limit512 MBPhil has a huge music collection and he is picky about what he listens to. Each song has a name that is a string of characters. His music player has a search box: type a string into it and the player lists every song whose name contains that string. When exactly one song matches, Phil presses Enter and that song plays.
Phil hates using the mouse and he hates typing, so for the song he wants right now he always types the shortest string that matches that one song and nothing else. Find that query for every song.
The first line contains the number of test cases T. Each test case starts with a line holding the number of songs N, followed by N lines with one song name each. These N names are Phil's whole collection.
A song name consists of letters, spaces and the hyphen character (-), and its length is between 1 and 100. Within one test case all song names are different. Names and the search are case insensitive, so "dZihan" and "Dzihan" are the same name.
Limits
For each test case print one line "Case #x:", where x is the test case number starting from 1. Then print one line per song, in the order the songs were given. For each song print, wrapped in double quotes, the shortest query that matches that song and no other song. If several shortest queries work, print the lexicographically smallest one. If no query works, print :( without quotes.
A query is a string of letters, spaces and hyphens, and the empty string is a valid query. The empty string is contained in every name.
In the lexicographic order a space comes before a hyphen, a hyphen comes before every letter, and upper case letters come before lower case letters.