Under construction

Still being built. Things may change

Guessing Game II

Time limit2sMemory limit128 MB

Summary
Given up to 8 guess/response pairs, decide whether the 4-digit secret is already unique, find the smallest next guess that identifies it, or report that none can.
Level

Medium7 of 10

Topics
Brute force, Hash map, Implementation, Math
Solved
No attempts yet

Problem

The secret to a gate is a 4-digit code, written with leading zeros so that it always has exactly four digits (from 0000 to 9999).

Someone has been making guesses at the code. Each guess is a 4-digit code, and the gate answers with two numbers:

  • circles — the number of positions at which the guessed digit equals the secret digit in that same position;
  • squares — the number of remaining guessed digits that occur in the secret but at a different position, counted with multiplicity. Formally, for each digit value dd let md=min⁡(count of d in the guess, count of d in the secret)m_d = \min(\text{count of }d\text{ in the guess},\ \text{count of }d\text{ in the secret}); then circles+squares=∑dmd\text{circles} + \text{squares} = \sum_d m_d.

You are given up to 8 recorded guess/response pairs for one secret. Using only these pairs, decide which of the following holds:

  1. The pairs already pin down the secret uniquely.
  2. The secret is not yet unique, but there exists one more guess whose response would tell it apart from every remaining possibility.
  3. Even one more guess cannot always reveal the secret.

A single guess uniquely identifies the secret when, over all codes still consistent with the recorded pairs, that guess produces a different response for every one of them — so whatever response the gate gives, exactly one candidate remains.

For example, suppose the recorded pairs are 5888 → 3 circles, 0 squares, 1234 → 0 circles, 0 squares, and 4567 → 0 circles, 0 squares. Only three codes are consistent: 0888, 8888, and 9888. Guessing 0009 next separates them (their responses are 1 circle, 0 circles, and 0 circles, 1 square), so 0009 identifies the secret. Guessing 0888 would not, because 8888 and 9888 would both answer 3 circles, 0 squares. Several guesses work here (0009, 0889, 0898, …); output the numerically smallest, 0009.

Input

The first line contains the number of test cases TT (T≤1000T \le 1000).

Each of the next TT lines describes one test case: it starts with the number of recorded guess/response pairs nn (0<n≤80 < n \le 8), followed by 3n3n integers. Each group of three integers is a triple guess, circles, squares.

Each guess is given as an integer and is implicitly padded on the left with zeros to four digits — for example the guess 1 means 0001. Tokens on a line may be separated by any amount of whitespace.

Output

For each test case, print exactly one line:

  • if the recorded pairs already determine the secret, print The secret is: SSSS, where SSSS is the secret as four digits;
  • otherwise, if some further guess can uniquely identify the secret, print There is a guess that can uniquely identify the secret: GGGG, where GGGG is that guess as four digits; if several guesses qualify, use the numerically smallest;
  • otherwise, print There is no guess that can uniquely identify the secret.

Examples3

  1. Example 1

    Input
    3
    1	1234 1 0 
    3	5888 3 0  	1234 0 0  	4567 0 0  
    4	5888 3 0  	1234 0 0  	4567 0 0  	0009 0 0
    
    Expected output
    There is no guess that can uniquely identify the secret.
    There is a guess that can uniquely identify the secret: 0009
    The secret is: 8888
    
  2. Example 2

    Input
    1
    1 1234 4 0
    
    Expected output
    The secret is: 1234
    
  3. Example 3

    Input
    1
    1 0000 4 0
    
    Expected output
    The secret is: 0000