Keywords

No attempts yetTime limit1sMemory limit128 MB

Problem

Researchers face an ever-growing number of journal articles and struggle to find the ones relevant to their work. A subscription service can help: you supply an interest profile, and the service forwards the articles that match it.

One simple way to match is to check whether a pair of keywords occurs sufficiently close to each other in an article's title. How close is "sufficient" is set by a threshold: the number of words that may appear between the two keywords. For example, the profile 0 rock art (threshold 0, keywords rock and art) asks for every title in which rock and art appear with 0 words between them — that is, right next to each other. It selects not only Rock Art of the Maori but also Pop Art, Rock, and the Art of Hang-glider Maintenance.

Read a series of profiles followed by a series of titles, and determine which titles (if any) each profile selects. A title is selected by a profile when at least one pair of the profile's keywords appears in the title separated by no more than the profile's threshold words. The two keywords of such a pair must be two different keywords of the profile.

Here a word is a maximal run of letters; words are separated by blanks or the end of a line.

Input

The input contains at most 50 profiles, followed by at most 250 titles. Profiles are numbered 1, 2, … in order of appearance, and titles are numbered 1, 2, … in order of appearance (these numbers do not appear in the file).

  • A profile is a single line that starts with P:, followed by an integer threshold and then two or more lowercase keywords, all separated by blanks.
  • A title starts with T: and is a string of characters ending with |. The character | appears only at the very end of a title. A title is at most 255 characters long and may wrap onto several lines; no line exceeds 80 characters, every continuation line begins with at least one blank, and line breaks fall only between words.

When a title is processed, all non-alphabetic characters are removed and letters are compared case-insensitively. For example Don't Rock --- the Boat as Metaphor in 1984 becomes the words dont rock the boat as metaphor in, and HP2100X becomes the single word hpx.

The input ends with a line containing a single #.

Output

Print one line for each profile, in input order. Each line contains the profile's number, then : and a single space, then the numbers of the titles it selects in increasing order, separated by commas with no spaces. If a profile selects no titles, print its number followed by : and a space, with nothing after.