#4986. Problem 2. Transforming Pairs

0

Problem 2. Transforming Pairs

Problem 2. Transforming Pairs

USACO 2025 February Contest, Platinum

Answer QQ (1Q1051\le Q\le 10^5) independent queries each of the following form:

You are given four integers a,b,c,da,b,c,d (1018a,b,c,d1018-10^{18}\le a,b,c,d\le 10^{18}). In one operation you can either do a+=ba\mathrel{+}=b, or b+=ab\mathrel{+}=a. Determine the minimum number of operations to transform (a,b)(a,b) into (c,d)(c,d), or if it is impossible to do so, output 1-1.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains QQ.

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

OUTPUT FORMAT (print output to the terminal / stdout):

The answer for each query on a separate line.

SAMPLE INPUT:


4
5 -3 -1 -3
5 3 5 2
5 3 8 19
5 3 5 3

SAMPLE OUTPUT:


2
-1
3
0

First query: (5,3)(2,3)(1,3)(5,-3)\to (2,-3)\to (-1,-3)

Second query: Impossible.

Third query: (5,3)(8,3)(8,11)(8,19)(5,3) \to (8, 3) \to (8, 11) \to (8, 19)

Fourth query: No operations necessary.

SCORING: Input 2: a,b,c,d10|a|, |b|, |c|,|d|\le 10Input 3: a,b0a,b\ge 0Input 4: a0ba \geq 0 \geq bInput 5: a0ba \leq 0 \leq bInput 6: a,b0a,b\le 0Input 7: c,d0c,d\ge 0Input 8: c0dc \geq 0 \geq dInput 9: c0dc \leq 0 \leq dInput 10: c,d0c,d\le 0Inputs 11-14: Q103Q \leq 10^3Inputs 15-19: No additional constraints.

Problem credits: Benjamin Qi