NAKANJ - Minimum Knight moves !!!


Anjali and Nakul are good friends. They both had a quarrel recently while playing chess. Nakul wants to know the minimum number of moves a knight takes to reach from one square to another square of a chess board (8 × 8). Nakul is brilliant and he had already written a program to solve the problem. Nakul wants to know whether Anjali can do it. Anjali is very weak in programming. Help her to solve the problem.

A knight can move in the shape of an "L" in a chessboard - two squares either forward, backward, left, or right and then one square to its left or right. A knight move is valid if it moves as mentioned above and it is within the boundary of the chessboard (8 × 8).

knight

Input

There are T test cases in total. The next T lines contain two strings (start and destination) separated by a space.

The strings start and destination will only contain two characters - First character is an alphabet between 'a' and 'h' (inclusive), Second character is a digit between '1' and '8' (inclusive) - (Quotes just for clarity).

To know the knight moves more clearly refer to the above figure.

Output

Print the minimum number of moves a knight takes to reach from start to destination in a separate line.

Constraints

1 <= T <= 4096

Example

Input:
3
a1 h8
a1 c2
h8 c3

Output:
6
1
4

hide comments
secret1: 2017-02-02 06:33:30

CAN IT BE SOLVED USING DFS?

a1796: 2017-01-01 21:54:11

ny first bfs :D

domino2016: 2016-11-26 16:05:07

Interesting problem

rishabh_nitw: 2016-10-17 05:31:08

basic bfs ...good to learn.

rohitranjan017: 2016-09-02 17:27:31

Easy BFS ! :)
My 100th.

Sarthak Munshi: 2016-08-30 20:38:56

Easy gridwalk . Solve SPOJ_NATALIAG before this .

Sunny: 2016-08-23 21:15:54

Implemented BFS in C++. Used visited array instead of maps for faster processing but still getting TLE.

praval_singhal: 2016-07-26 11:58:38

After 8 wrong submissions I realized that chess board is 8*8 not 64*64. Finally After scanning my code bottom to top finally AC :P

kushalanand: 2016-07-24 16:14:27

routine bfs. increased conditions. AC in one go. :D

pvsmpraveen: 2016-07-08 16:53:41

BFS and 0.00 :)


Added by:Nakul Krishna
Date:2012-09-30
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Used for Code it - Vidyut 2012 - Amrita University