Chess#815

Chess

3 seconds
256 megabytes
Very hard

Let's review some fundamental chess rules relevant to this problem. Chess is played by two players: one with the white pieces and the other with the black. The game is played on an 8 \times 8 board. The columns are labeled from to from left to right, and the rows are numbered from to from bottom to top. Each square on the board may be empty or occupied by a single piece. If a piece (not a pawn) can move to a square occupied by an opponent's piece according to the rules, piece is captured and removed from the board. Thus, all squares a piece can move to are considered "under attack" by that piece. The king cannot move to any square under attack by an opponent's piece. If a player makes a move that places the opposing king under attack (this is called "check"), the opponent must make a move so that their king is no longer under attack. If no such move exists, it is called "checkmate."

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

There are three pieces on the chessboard: a white king, a white queen, and a black king. It is white's turn to move. What is the minimum number of moves required to guarantee checkmate? The black king will do everything allowed by the rules to avoid checkmate.

Input

The program should read the number , the number of test cases, followed by the test cases themselves. Each test case is a line containing three square designations, indicating the positions of the white king, white queen, and black king, respectively, separated by spaces (each square is designated by the column letter and the row number together).

All given positions are valid according to chess rules (for example, the black king is not under attack).

Output

For each test case, output a single number: the minimum number of moves required.

Examples

Example 1

Loading
Loading