CRAN01 - An Experiment by Penny

Penny started studying in a community college. However she did not tell Leonard about this because she did not want Leonard helping her at every point in her studies. This went well until the professor ordered her to perform an advanced experiment. In this experiment she was given an advanced microbiological specimen. This specimen is placed in an n × m size grid which is divided into 1 × 1 cells.

It expands according to following rules.

If at time t, the specimen occupies (x, y), then at time t + 1 it can expand to at most any two cells out of (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1).

For example if at t = 0 sec if the specimen occupies (4, 5), then at time t = 1 sec, the state of the grid can be any of the following:

  1. specimen at (4, 5), (5, 5) and (3, 5).
  2. specimen at (4, 5), (5, 5) and (4, 6).
  3. specimen at (4, 5), (5, 5) and (3, 4).
  4. specimen at (4, 5), (3, 5) and (4, 6).
  5. specimen at (4, 5), (3, 5) and (4, 4).
  6. specimen at (4, 5), (4, 6) and (4, 4).

Note - At t = 2 sec, it can expand from all the points that the specimen occupied at t = 1.

The professor asks penny to find the minimum time it takes for the specimen to fill the entire grid.

Since penny is not so smart at math and she can't ask Leonard to help her, she turns to you for help and to find the solution to above problem.

Input

T - The number of test cases.

n m - number of row and columns in the grid.

x y - coordinate of the initial position of the specimen.

Output

The minimum time in seconds it takes for specimen to fill in the entire grid.

Constraints

1 <= T <= 50

1 <= n, m <= 500

1 <= x <= n

1 <= y <= m

Example

Input:
2
1 1
1 1
10 10
6 4 Output: 0
11

Added by:CSI
Date:2013-02-16
Time limit:3s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64

hide comments
2013-06-30 13:17:07 Anubhav Balodhi
< snip > i'm gettin' wa even if i tested many cases wid my both algorithms...
--edit(francky)--> Please don't use gibberish.
(francky) please check my codes, if you can... I've satisfied all cases that i could.

Last edit: 2013-08-25 09:36:40
2013-06-05 07:12:38 Sandeep Sharma
Piece of Cake :)
2013-05-05 05:20:55 Jitesh
very easy ;)
2013-05-03 14:57:23 Arika Saputro
simple logic ;D
2013-03-11 15:55:32 Man Mohan Mishra
easy , but nice problem :)
2013-02-24 00:57:59 Aditya Bahuguna
nice logic ;)
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.