#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int T;
long long int X, Y, P, Q;

long long int INF = 8300000000000000008;


long long int gcdExt(long long int a, long long int b, long long int &x, long long int &y) {

	if (b == 0) {
		x = 1; y = 0;
		return a;
	}
	long long int X, Y;
	long long int d=gcdExt(b, a%b, X, Y);
	x = Y;
	y = X - a / b*Y;

	return d;
}
/*
pair<long long int, long long int> ChineseRem(long long int y1, long long int m1, long long int y2, long long int m2) {

	long long int p, q;
	long long int d = gcdExt(m1, m2, p, q);

	if (abs(y2 - y1) % d != 0) { return make_pair(0, -1); }

	long long int m = (m1 / d)*m2;

	long long int tmp = ((y2 - y1) / d)*m1; 
	
	tmp %= m;  tmp = (tmp + m) % m;
	
	tmp *= (p%m); tmp %= m; tmp = (tmp + m) % m;
	
	return make_pair((y1 + tmp) % m, m);

}
*/

// 負の数にも対応した mod
// 例えば -17 を 5 で割った余りは本当は 3 (-17 ≡ 3 (mod. 5))
// しかし単に -17 % 5 では -2 になってしまう
inline long long mod(long long a, long long m) {
	return (a % m + m) % m;
}

// 拡張 Euclid の互除法
// ap + bq = gcd(a, b) となる (p, q) を求め、d = gcd(a, b) をリターンします
long long extGcd(long long a, long long b, long long &p, long long &q) {
	if (b == 0) { p = 1; q = 0; return a; }
	long long d = extGcd(b, a%b, q, p);
	q -= a / b * p;
	return d;
}

// 中国剰余定理
// リターン値を (r, m) とすると解は x ≡ r (mod. m)
// 解なしの場合は (0, -1) をリターン
pair<long long, long long> ChineseRem(long long b1, long long m1, long long b2, long long m2) {
	long long p, q;
	long long d = extGcd(m1, m2, p, q); // p is inv of m1/d (mod. m2/d)
	if ((b2 - b1) % d != 0) return make_pair(0, -1);
	long long m = m1 * (m2 / d); // lcm of (m1, m2)
	long long tmp = (b2 - b1) / d * p % (m2 / d);
	long long r = mod(b1 + m1 * tmp, m);
	return make_pair(r, m);
}

int main(void) {

	long long int y1, m1, y2, m2;

	/*cin >> y1 >> m1 >> y2 >> m2;
	auto ret = ChineseRem(y1, m1, y2, m2);
	cout << ret.first<<" "<<ret.second << endl;
	
	
		
		 return 0;*/
	cin >> T;
	for (int t = 1; t <= T; t++) {
		cin >> X >> Y >> P >> Q;

		long long int Ts = X * 2 + Y * 2;
		long long int Ss = P + Q;


		long long int ans = INF;
		for (long long int y = X; y <= X + Y - 1; y++) {
			for (long long int q = P; q <= P + Q - 1; q++) {

				auto ret = ChineseRem(y, Ts, q, Ss);

				if (ret.second == -1) { continue; }
				else {
					ans = min(ans, ret.first);
				}
			
					
			}// q-loop

		}//y-loop


		if (ans >= INF) { cout << "infinity" << endl; }
		else { cout << ans << endl; }
	}


	return 0;
}
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i, a, b) for(int i = (a); i <= (b); i++)
#define PER(i, a, b) for(int i = (a); i >= (b); i--)
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define all(S) (S).begin(), (S).end()
#define pb push_back
#define mk make_pair
#define S second
#define F first
typedef long long ll;
typedef long double lf;
typedef pair<int, int> ii;

// Euclides Estendido
ll gcd(ll A, ll B, ll &X, ll &Y) {
	if(B == 0) {
		X = 1; Y = 0;
		return A;
	}
	ll x1, y1;
	ll G = gcd(B, A % B, x1, y1);
	X = y1; Y = x1 - (A / B) * y1;
	return G;
}

// Acha a primeira vez que dois eventos ocorrem ao mesmo tempo
// Os eventos acontecem a cada:
// A * T1 + Z1 e B * T2 + Z2
ll solve(ll A, ll B, ll Z1, ll Z2) {
	if(Z2 > Z1) swap(A, B), swap(Z1, Z2);
	ll X, Y, ans = 0;
	ll G = gcd(A, B, X, Y), C = Z2-Z1;
	if(C%G) return 1LL << 62;	// impossivel
	C /= G; X *= C; Y *= C;
	// Acho o primeiro X positivo
	if(X >= 0) {
		ll K = (X * G) / B;
		ans = A * (X - K * (B / G)) + Z1;
	} else {
		ll K = (-X * G + B - 1) / B;
		ans = A * (X + K * (B / G)) + Z1;
	}
	// retorna um par na forma (A, Z)
	// A -> novo tamanho do ciclo
	// Z -> primeira vez que acontece
	// return mk(A * (B / G), ans);
	return ans;
}

// Anotação importante, Há duas formas de alterar as soluções:
// (1) -> X = X + K * (B / G) e Y = Y - K * (A / G)
// (2) -> X = X - K * (B / G) e Y = Y + K * (A / G)

int main(int argc, char** argv) {
	int T, X, Y, P, Q;
	scanf("%d", &T);
	rep(t, 0, T) {
		ll ans = (1LL <<62);
		scanf("%d%d", &X, &Y);
		scanf("%d%d", &P, &Q);
		rep(y, 0, Y) rep(q, 0, Q)
			ans = min(ans, solve(2*X+2*Y, P+Q, X + y, P + q));
		if(ans != (1LL << 62))
			printf("%lld\n", ans);
		else puts("infinity");
	}
	return 0;
}

