Washington Distance

Time limit: 0.1s Memory limit: 64MB Input: Output:

Ethan and George are in Washington D.C. for their favorite debate tournament’s final round. To get to the venue, they need to use a taxi, and the driver expects the directions given according to the city’s unique street system described below. The starting point and destination is an intersection of two streets.

The streets of Washington form a rectangular grid, which can be modeled as vertical and horizontal lines in the planar coordinate system. There is a street parallel to the OxOx and OyOy axis on each integer coordinate from 25−25 to 2525. However, the streets are numbered in a very strange way.

Firstly, the city is divided into four quadrants: NWNW, SWSW, NENE, SESE (north-west, south-west, north-east, south-east), relative to the center of the city (which corresponds to the origin in the coordinate system). The vertical streets are identified by letters from AA to ZZ. Street AA goes through the center, and on both the eastern and western sides the streets are named BB, CC, ..., ZZ in the order of distance from the center. The horizontal streets are identified by numbers between 00 and 2525. Street 00 goes through the center, and in both the northern and southern halves, the streets are numbered from the bottom to the top with increasing numbers from 11 to 2525 (so the numbers reflect the remainder of the real coordinate when divided by 2626). See the image below for a better understanding.

Now, knowing this, Ethan and George want to know the distance between TT pairs of points, according to the Washington Coordinate system. The distance means the length of the ride with the taxi, which can only travel along the streets, given in units of the coordinate system.

Input data

The first line of the input contains TT, the number of test cases (1T104)(1 \leq T \leq 10^4).
The next TT lines of the input contain two addresses according to the Washington Coordinate System, given by their direction, letter and street number.

Output data

You need to write a single line with an integer: the unique integer that solves this task.

Constraints and clarifications

  • 1T1041 \leq T \leq 10^4;

Your program will be tested against several test cases grouped in subtasks. In order to obtain the score of a subtask, your program needs to correctly solve all of its test cases.

# Points Constraints
1 0 Examples
2 20 All addresses are in the NENE quadrant
3 80 No additional limitations

Example

stdin

5
NE A 5 NE B 7
NE G 15 SW P 0
SE U 5 NW Q 10
NW A 19 SW B 3
SE Q 21 SW P 4

stdout

3
36
67
43
48

Explanation

The situation of the first sample case can be seen below: (clearly the distance is 33)

In the second sample case the taxi driver does not have to cross line 00, but must cross line AA. The distance between line GG (corresponding to the starting point) and line AA is 66. The distance between line PP (corresponding to the end point) and line AA is 1515. So the total distance is 6+15+15=366 + 15 + 15 = 36.

In the third sample case the taxi driver must cross both line 00 and line AA. The distance between line UU (corresponding to the starting point) and line AA is 2020. The distance between line QQ (corresponding to the end point) and line AA is 1616. So the total distance is 20+16+21+10=6720 + 16 + 21 + 10 = 67 as the distance between line 55 (corresponding to the starting point) and line 00 is 2121.

Log in or sign up to be able to send submissions!