Chess#5853

Chess

4 seconds
64 megabytes
Very hard

Let's recall some essential points from the rules of chess. The game is played by two players: one plays with the white pieces and the other with the black pieces. The game is played on an 8 \times 8 board, with columns labeled from a to h (left to right) and rows numbered from 1 to 8 (bottom to top). Each square on the board is either empty or contains exactly one piece. If a piece (not a pawn) can legally move to a square occupied by piece , then as a result of this move piece is captured (removed from the board). Thus, all squares that a certain non-pawn piece can move to are said to be "under attack" by that piece. The king is not allowed to move to any square that is under attack by an opponent's piece.

If a player makes a move such that the opponent's king comes under attack, this situation is called "check". The opponent must respond in such a way that their king is no longer under attack after their move. If this is not possible, it is called "checkmate".

The king can move one square in any of directions (left, right, forward, backward, or any diagonal). The queen can move in any of directions for any number of squares, but cannot move through squares occupied by other pieces.

Suppose there are three pieces on the chessboard: the white king, the white queen, and the black king. It is White's turn to move. What is the minimum number of moves required to guarantee checkmate? Black will make all legal moves to avoid being checkmated.

Input

The program should first read one integer (1 \leq \mathrm{TEST\_NUM} \leq 70000) — the number of test cases — then the test cases themselves. Each test case is a separate line containing the notations of three squares where the white king, white queen, and black king are located. The notation for a square consists of the column letter followed by the row number; the three notations in the line are separated by a single space.

All test cases are guaranteed to be valid according to the rules of chess (for example, the black king is not under attack).

Output

For each test case, your program should output a single integer — the minimum number of moves required.

Examples

Example 1

Loading
Loading