Checkmate with Two Rooks

No attempts yetTime limit5sMemory limit128 MB

Problem

Sanggeun and Jeongin sat down for a game of chess and found the white king missing. They decided to play a modified game instead. Sanggeun has two rooks (♖) and Jeongin has only a king (♚). It is Sanggeun's turn.

Sanggeun wants to win by checkmate and Jeongin defends as well as he can. Jeongin avoids checkmate whenever he can, and when he cannot, he stretches the game out for as many of Sanggeun's moves as possible. Given that both play this way, find the smallest number of moves Sanggeun needs to win.

The rules are these.

  1. A position like this cannot arise in real chess. Apart from that, everything follows the rules of chess.
  2. The board is 8×88 \times 8. Ranks are numbered 1 to 8 and files are lettered a to h, so c7 is the square where file c meets rank 7.
  3. Only Sanggeun's moves are counted.
  4. A rook moves any number of squares along a rank or a file. It cannot pass over the other rook or the king, and it cannot stop on the square of the other rook.
  5. A check is a white rook standing where it could take the king.
  6. The king moves one square in any of the eight directions, but never onto a square where it would be taken. It may move onto a rook's square and capture that rook, unless the remaining rook covers that square.
  7. Checkmate is a position where the king is in check and has no legal move. Sanggeun wins here.
  8. Stalemate is a position where the king is not in check and still has no legal move. The game is a draw.
  9. A single rook cannot checkmate a lone king, so once Jeongin captures a rook, Sanggeun can no longer win.

Input

The first line contains the number of test cases.

Each test case is one line holding the square of Jeongin's king followed by the squares of Sanggeun's two rooks, separated by spaces. A square is written as a lowercase letter from a to h for the file, then a digit from 1 to 8 for the rank.

No two pieces stand on the same square, and Jeongin's king is never in check in the given position.

Output

For each test case, print the smallest number of moves Sanggeun needs to win, one per line. Print 0 if he cannot win.