Safebreaker

Interview

Time limit1sMemory limit128 MB

Summary
Given up to 10 guesses with correct-position and wrong-position digit counts, decide whether the secret 4-digit code is unique, impossible, or indeterminate.
Level

Medium4 of 10

Topics
Brute force, Implementation, Simulation, Array
Solved
No attempts yet

Problem

We are watching someone play a game similar to Mastermind. The goal is to find a secret code by clever guessing, guided by clues. The secret code is a 4-digit number in the inclusive range from 0000 to 9999, for example 3321.

The player makes a guess, for example 1223, and receives a clue describing how close the guess is. A clue consists of two numbers:

  • the number of correct digits (digits that are right and in the right position), and
  • the additional number of digits that are guessed correctly but in the wrong place.

For the secret 3321 and the guess 1223, exactly one digit is correct and in place (the 2 in the third position) and two more digits are guessed correctly but misplaced (the 1 and the 2), so the clue is 1/2. For the guess 1110, the clue is 0/1: no digit is in the right position, and only a single 1 is correct but misplaced.

Given a set of guesses together with their clues, write a program that determines the secret code.

Input

The first line contains the number of test cases NN.

Each test case begins with a line containing the number of guesses GG (0≤G≤100 \le G \le 10). The next GG lines each contain exactly 8 characters: a 4-digit code, a single blank, one digit giving the number of correct digits, a /, and one digit giving the number of digits that are correct but in the wrong place.

Output

For each test case, print a single line:

  • impossible if no code is consistent with all of the guesses;
  • the secret code, as a 4-digit number keeping any leading zeros, if exactly one code is consistent with all of the guesses;
  • indeterminate if more than one code is consistent with all of the guesses.

Examples1

  1. Example 1

    Input
    4
    6
    9793 0/1
    2384 0/2
    6264 0/1
    3383 1/0
    2795 0/0
    0218 1/0
    1
    1234 4/0
    1
    1234 2/2
    2
    6428 3/0
    1357 3/0
    
    Expected output
    3411
    1234
    indeterminate
    impossible