Bulls and Cows

Time limit1sMemory limit128 MB

Problem

Bulls and Cows is a game for two players: the codemaker and the codebreaker. The codemaker chooses a secret code — a sequence of $K$ decimal digits that are all different (a leading zero is allowed). The codebreaker then tries to determine the code by naming numbers of $K$ digits.

For every guess, the codemaker replies with two counts:

  • Bulls — the number of digits whose value and position are both correct.
  • Cows — the number of digits whose value is correct but whose position is wrong.

For example, if the secret code is 1230 and the codebreaker guesses 1205, the reply is 2 Bulls and 1 Cow.

Given the full transcript of guesses and replies for one game, report how many codes are still possible and, among all of them, the numerically smallest one.

Input

The input contains one or more game descriptions.

Each game description starts with a line containing a single integer $K$ with $1 \le K \le 7$. A value of $K$ that is not positive marks the end of the input.

Each positive $K$ is followed by zero or more guess-and-reply lines. Every such line holds $K+2$ integers separated by one or more spaces: the first $K$ integers are the digits of a guess, the next integer is the number of Bulls, and the last integer is the number of Cows. The list of guess-and-reply lines ends with a line whose first integer is $-1$ (any remaining integers on that line are ignored).

Output

For each game, print exactly one line of the form

N is one of M possible solutions.

where $N$ is the numerically smallest code consistent with every guess and reply — written as $K$ digits with no separators and keeping any leading zeros — and $M$ is the number of codes consistent with the whole transcript.