Theta Puzzle#2448

Theta Puzzle

1 segundo
64 megabytes
Media (≈1350)

Theta Puzzle consists of a base with 6 positions at the vertices of a regular hexagon and another position at the center, connected as shown in the figure below. There are six tokens labeled A, B, C, D, E, and F. A single move of the puzzle is to move a token to an adjacent empty position (along an allowed connection — the line segments in the diagram below). The goal of the puzzle is to start with an initial arrangement of tokens with the center empty and, by a sequence of moves, reach the configuration shown in the figure below.

The initial position for the puzzle is given by a permutation of the letters A through F. The first letter is placed at A in the figure, the next at B, and so on.

A sequence of moves is specified by listing the labels of the tokens to be moved, in the order they are to be moved.

For example, to solve the puzzle FACDBE, use the moves BEFAB.

Note: Not all starting permutations can be solved.

Write a program which, given an initial permutation, finds the shortest sequence of moves to solve the puzzle or determines that there is no solution.

Entrada

The first line contains a single integer (1 \leq P \leq 1000), the number of data sets. Each data set is a single line: the data set number, a space, and a permutation of the letters through giving the initial puzzle position.

Salida

For each data set, output a single line. If there is no solution, the line should contain the data set number, a space, and the string NO SOLUTION. If there is a solution, the line should contain the data set number, a space, the number of moves, a space, and the solution as a string of the letters through . If the number of moves is zero (), output the space after , even though there is no string of letters.

Ejemplos

Ejemplo 1

Loading
Loading