CYLINDES - Shortest Path on a Cylinder

no tags 

Ant Smart is on a surface of cylinder now. He wants to move to another position of the cylinder’s surface. Like many other animals named Smart, he wants to find out the shortest path from one point to another.

Unfortunately, Ant Smart is not enough smart to solve this question now. It is your task to find out the answer.

Input

There are several test cases in this problem. The number of them is about 1000. The first line of input contains a single integer denoting the number of test cases.

For each test case, the first line contains two integers - the radius and height of the cylinder, separated by spaces.

For the next two lines, each line contains three integers: h, a and r (0 <= h <= height, 0 <= a < 360, 0 <= r <= radius), denoting one point on the surface of cylinder, respectively. h indicates a circle on the surface of cylinder which apart h from the bottom. And the polar angle a and radius r indicates the position of the point on the circle. In the other words, if the cylinder is (0,0,0) - (0,0,height) on the 3D grid coordinate. The point can be represented as (cos(a)*r, sin(a)*r, h).

You may assume that the given points must be on the surface of the given cylinder. All input numbers are non-negative and less than or equal to 1000.

Output

For each test case, output only one line contains the length of the shortest path on the surface of cylinder. We accept solutions with absolute error less than 10-2 in at least 99% of the test cases.

Example

Input:
2
5 10
10 0 3
5 0 5
90 49
49 312 39
0 52 65

Output:
7.00
171.02

hide comments
Alex Anderson: 2013-02-11 21:12:22

Hmm, I think I have an error of ~10^0...

Alex Anderson: 2013-02-10 05:40:57

What are the limits on the height and radius of the cylinder? Or it is relatively unbounded (fits in signed 32 bit int?)

:D: 2012-12-12 13:26:44

Cylinder also has two bases, not only the side.

N Hari Prasad: 2012-07-25 09:42:46

If the ant is on the surface of the cylinder, shouldn't r be always equal to radius of the cylinder?

Alex Anderson: 2011-12-01 02:01:02

I don't really understand what you mean by surface. In test case 1, the first point 10 0 3 is clearly not on the surface of the cylinder. Similarly for both points in the second test case.

RE (by XilinX): Read the problem statement CAREFULLY! The input numbers are not the coordinates in the Cartesian coordinate system.

EDIT by me: Thanks, I had tricked myself into misunderstanding the input.

Last edit: 2011-09-11 02:35:32

Added by:Fudan University Problem Setters
Date:2011-09-08
Time limit:20s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Fudan University Local Contest #3, by g201513