#P10746. [SEERC 2020] Codenames

[SEERC 2020] Codenames

Problem Description

There are qq 5×55 \times 5 boards. For each cell, its color is given (only r, b, n, x are possible). The color distribution is: 99 r, 88 b, 55 n, and 11 x.

The character corresponding to each cell on the board is fixed as follows:

abcde
fghij
klmno
pqrst
uvwxy

On the board, some colors have already been revealed (a revealed cell is shown using the uppercase letter of its color). Then you need to choose a string ww from a list of nn strings and a number gg, and perform the following operation gg times.

  • If the cell corresponding to the current character wiw_i has already been revealed, do nothing and set ii+1i \gets i + 1.
  • Otherwise, reveal the cell corresponding to wiw_i. If the color of wiw_i is n, b, or x, you lose. Then set ii+1i \gets i + 1.
  • Stop when gg operations are completed or when all r cells have been revealed.

You want to reveal all r cells. Find a pair (w,g)(w, g) that satisfies this requirement.

Input Format

The first line contains an integer n (1n105)n\ (1 \leq n \leq 10^5).

Then follow nn lines, each containing a string, describing the whole list of strings.

Line n+2n + 2 contains an integer q (1q105)q\ (1 \leq q \leq 10^5), the number of boards.

Then follow qq boards, each of size 5×55 \times 5, describing the colors of the ii-th board.

Output Format

For each board, if there is a solution, output one valid ww and g (1g9)g\ (1 \leq g \leq 9). If there is no solution, output IMPOSSIBLE.

3
actor
cheat
zeta
1
rBBnR
NRnbB
nRRnR
NRxBr
nBRbB
actor 2

Hint

The answer could also be actor 4, zeta 2, etc.

Translated by ChatGPT 5