Transforming Pairs

시간 제한2초메모리 제한2048 MB

요약
두 양의 수 (a,b)에서 한 수를 두 수의 합으로 바꾸는 연산만 쓸 수 있을 때, (c,d)로 만드는 최소 연산 횟수를 각 질의마다 구하고 불가능하면 -1을 출력한다.
난이도

어려움10점 중 8점

유형
수학, 정수론, 그리디, 구현
정답자
아직 제출이 없습니다

문제

Bessie the brilliant bovine has discovered a new fascination—mathematical magic! One day, while trotting through the fields of Farmer John’s ranch, she stumbles upon two enchanted piles of hay. The first pile contains aa bales, and the second pile contains bb bales (1≤a,b≤10181\le a,b\le 10^{18}).

Next to the hay, half-buried in the dirt, she discovers an ancient scroll. As she unfurls it, glowing letters reveal a prophecy:

To fulfill the decree of the Great Grasslands, the chosen one must transform these two humble hay piles into exactly cc and dd bales—no more, no less.

Bessie realizes she can only perform the following two spells:

  • She can magically conjure new bales to increase the first pile’s size by the amount currently in the second pile.
  • She can magically conjure new bales to increase the second pile’s size by the amount currently in the first pile.

She must perform these operations sequentially, but she can perform them any number of times and in any order. She must reach exactly cc bales in the first pile and dd bales in the second pile (1≤c,d≤10181\le c,d\le 10^{18}).

For each of TT (1≤T≤1041\le T\le 10^4) independent test cases, output the minimum number of operations needed to fulfill the prophecy, or if it is impossible to do so, output -1.

입력

The first line contains TT.

The next TT lines each contain four integers a,b,c,da,b,c,d.

출력

Output TT lines, the answer to each test case.

예제2

  1. 예제 1

    입력
    4
    5 3 5 2
    5 3 8 19
    5 3 19 8
    5 3 5 3
    
    예상 출력
    -1
    3
    -1
    0
    
  2. 예제 2

    입력
    1
    1 1 1 1000000000000000000
    
    예상 출력
    999999999999999999