#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e18 + 10;
int t;
int a, b, c, d;
int solve(int a, int b, int c, int d)
{
if (c < 0 || d < 0) return -1;
if (a == c && b == d) return 0;
int res = 0;
while (c != 0 && d != 0)
{
if (a == c && b == d) return res;
if (c < d)
{
swap(c, d);
swap(a, b);
}
if (b == d && c >= a && c % d == a % d) return res + (c - a) / d;
if (c % d == 0 && ((!a && b == d) || (a == d && !b))) return res + c / d;
res += c / d;
c %= d;
}
if (a == c && b == d) return res;
return -1;
}
int solve2(int a, int b, int c, int d)
{
int tmp = solve(a, b, c, d);
return tmp == -1 ? INF : tmp;
}
signed main()
{
cin >> t;
while (t -- )
{
cin >> a >> b >> c >> d;
if (a == c && b == d)
{
puts("0");
continue;
}
if (a <= 0 && b <= 0)
{
a = -a;
b = -b;
c = -c;
d = -d;
}
if (a < 0)
{
swap(a, b);
swap(c, d);
}
if (a >= 0 && b >= 0)
{
if (c < 0 || d < 0)
{
puts("-1");
continue;
}
cout << solve(a, b, c, d) << "\n";
continue;
}
if (c < 0 && d > 0)
{
puts("-1");
continue;
}
if (c >= 0 && d <= 0)
{
cout << solve(c, -d, a, -b) << "\n" ;
continue;
}
if (c < 0 && d < 0)
{
a = -a;
b = -b;
c = -c;
d = -d;
}
if (a < 0)
{
swap(a, b);
swap(c, d);
}
vector<array<int, 3>> q;
int num = 0;
for (int x = c, y = d ; x > 0 && y > 0 ; )
{
if (y >= x)
{
num += y / x;
y %= x;
}
else
{
q.push_back({y, x, num});
num += x / y;
x %= y;
}
}
int ans = INF;
num = 0;
while (a > 0 && b < 0)
{
if (a + b == 0)
{
ans = min(ans, num + 1 + solve2(a, 0, c, d));
break;
}
if (a + b < 0)
{
num += (-b) / a;
b = - ((-b) % a);
continue;
}
for (auto item : q)
{
int y = item[0], nx = item[1];
if (y <= a + b && (a - y) % (-b) == 0)
{
int k = (a + b - y) / (-b);
int x = a + k * b;
if (x <= nx && (nx - x) % y == 0)
ans = min(ans, num + item[2] + 1 + k + (nx - x) / y);
}
}
if (a % (-b) == 0)
ans = min(ans, num + a / -b + solve2(-b, 0, c, d));
num += a / (-b);
a %= (-b);
}
if (a >= 0 && b >= 0)
ans = min(ans, num + solve2(a, b, c, d));
if (ans >= INF) puts("-1");
else cout << ans << "\n";
}
return 0;
}