Hilbert's Hedge Maze
문제
David has elaborate hedge mazes in his garden. For each of his mazes of different sizes, he wonders how long it would take to walk between any pair of locations in and around that maze. A hedge maze of order is constructed according to very specific instructions:
- Start with .
- Repeat times:
- Create a new string by replacing each in with and each with .
- Set .
- Remove all 's and all 's from .
The resulting string gives the instructions for constructing the hedge maze. In the unbounded plane start at coordinates , facing towards and for every move forward one unit, for every turn right degrees, and for every turn left degrees.
After constructing the maze, identify position with the unit square cell bounded by coordinates . We can move directly between two positions , if and only if either or , but not both, and there is no hedge between these cells. The distance between these two positions is .
Given a number and a pair of positions, find the shortest distance between these positions.
입력
Input starts with a positive integer , denoting the number of test cases. Each of the next lines consists of integers , , , , , satisfying , and . There is no interaction between the test cases. Each specifies a single hedge maze situated alone in the unbounded plane.
출력
Output lines with one integer on each line, corresponding to the shortest distance between the two positions in each of the test cases.
힌트
The shortest path from the first case in the sample input.
예제
예제 1
2 3 5 3 4 0 2 4 3 0 2
16 11