欢迎光临散文网 会员登陆 & 注册

CF 1806A. Walking Master

2023-06-08 20:40 作者:您是打尖儿还是住店呢  | 我要投稿

YunQian is standing on an infinite plane with the Cartesian coordinate system on it. In one move, she can move to the diagonally adjacent point on the top right or the adjacent point on the left.That is, if she is standing on point (x,y)

, she can either move to point (x+1,y+1)

 or point (x−1,y)

YunQian initially stands at point (a,b)

 and wants to move to point (c,d)

. Find the minimum number of moves she needs to make or declare that it is impossible.


Input

The first line contains a single integer t (1≤t≤104) — the number of test cases. The description of test cases follows.

The first line and only line of each test case contain four integers a, b, c, d (−108≤a,b,c,d≤108).

Output

For each test case, if it is possible to move from point (a,b) to point (c,d)

, output the minimum number of moves. Otherwise, output −1

Example

input

6

-1 0 -1 2

0 0 4 5

-2 -1 1 1

-3 2 -3 2

2 -1 -1 -1

1 1 0 2

outputCopy

4

6

-1

0

3

3

Note

In the first test case, one possible way using 4 moves is (−1,0)→(0,1)→(−1,1)→(0,2)→(−1,2)

. It can be proven that it is impossible to move from point (−1,0) to point (−1,2) in less than 4

 moves.

就是判断移动的次数跟能不能移动到,,主要是先判断Y值的大小,因为只能想右上方跟左边行走,所以判断y值的大小变化,再根据y值大小变化去比较x值的大小,即可返回;下面是代码:


CF 1806A. Walking Master的评论 (共 条)

分享到微博请遵守国家法律