R9K

Remove known nicknames and collapse whitespace in each message, then report whether the resulting text has appeared before.

Medium4StringHash mapImplementationSimulationNo attempts yetTime limit2sMemory limit512 MB

Problem

Mirko recently discovered IRC (Internet Relay Chat) and quickly became a moderator of several popular channels. Spammers are a big problem for him: they flood a channel with the same message over and over, and the other users cannot hold a real conversation. Mirko heard about an automatic moderation method called "R9K mode" and asked you to help him implement it. In R9K mode, repeated messages are blocked automatically, with one extra condition: every nickname that appears in a message is ignored.

The method works exactly as follows.

  • A nickname is the name a user goes by in the channel.
    • A nickname is a non-empty string of consecutive digits and uppercase or lowercase English letters.
    • Uppercase and lowercase letters are different.
    • The list of possible nicknames is known in advance.
    • Examples of valid nicknames are Mirko, Slavko, 0cool, and AcidBurn.
  • Messages arrive in the channel one after another.
    • A message consists of spaces, digits, uppercase and lowercase English letters, and some punctuation marks (period ., hyphen -, comma ,, semicolon ;, question mark ?, and exclamation mark !).
    • A message cannot start or end with a space.
    • A message can be empty.
    • Examples of valid messages are Mirko i Slavko ce biti na CERC-u. and Puno srece na natjecanju zele vam organizatori!.
  • First, every occurrence of every nickname is deleted from each message.
    • A nickname is delimited by a space, a punctuation mark, or the start or end of the message. In other words, a maximal run of letters and digits is deleted only if the whole run equals a nickname.
    • For example, if the nicknames are Mirko and Slavko, the message Mirko i Slavko ce biti na CERC-u. becomes _i__ce_biti_na_CERC-u. (an underscore stands for a space), and the message !Mirko.Slavko? becomes !.?. Nothing is deleted from the message MirkoSlavko.
  • Every run of two or more consecutive spaces is replaced with a single space.
    • For example, _i__ce_biti_na_CERC-u. becomes _i_ce_biti_na_CERC-u. (an underscore stands for a space).
  • Spaces at the start and at the end of the message are deleted.
    • For example, _i_ce_biti_na_CERC-u. becomes i_ce_biti_na_CERC-u. (an underscore stands for a space).
  • For each message, in the order the messages arrive, print BRISI if the transformed message has appeared before, and OSTAVI if it has not.

Input

The first line contains two positive integers NN and MM: the number of nicknames and the number of messages.

The second line contains the NN nicknames, separated by spaces.

Each of the next MM lines contains one message, in the order the messages arrived in the channel.

The whole input has fewer than 2 million characters in total, and the nicknames and messages follow the rules given in the statement.

Output

Print MM lines, one for each message. On each line, print BRISI if the message must be deleted or OSTAVI if it is kept.