#include <cstdio>
#include <iostream>
using namespace std;
int t;
long long a;
long long b;
long long c;
long long d;
long long res;
int main () {
freopen ("Transforming.in", "r", stdin);
freopen ("Transforming.out", "w", stdout);
cin >> t;
for (int index = 1; index <= t; index++) {
cin >> a >> b >> c >> d;
if (a > c || b > d) {
cout << -1 << endl;
continue;
}
res = 0;
while (true) {
if (a == c && b == d) {
cout << res << endl;
break;
}
if (c > d) {
long long t = (c - a) / d;
if (!t) {
cout << -1 << endl;
break;
}
res += t;
c -= t * d;
}
else {
long long t = (d - b) / c;
if (!t) {
cout << -1 << endl;
break;
}
res += t;
d -= t * c;
}
}
}
return 0;
}