#include <bits/stdc++.h>
using namespace std;
template<class T, class U> T &ctmax(T &x, const U &y){ return x = max<T>(x, y); }
template<class T, class U> T &ctmin(T &x, const U &y){ return x = min<T>(x, y); }



constexpr pair<long long, long long> inv_gcd(long long a, long long b) {
	a = a % b;
	if(a < 0) a += b;
	if(a == 0) return {b, 0};
	long long s = b, t = a;
	long long m0 = 0, m1 = 1;
	while(t){
		long long u = s / t;
		s -= t * u;
		m0 -= m1 * u;
		auto tmp = s;
		s = t;
		t = tmp;
		tmp = m0;
		m0 = m1;
		m1 = tmp;
	}
	if(m0 < 0) m0 += b / s;
	return {s, m0};
}

// (rem, mod)
// Requires inv_gcd
pair<long long, long long> chinese_remainder_theorem(const vector<long long> &r, const vector<long long> &m){
	assert(r.size() == m.size());
	int n = (int)r.size();
	long long r0 = 0, m0 = 1;
	for(int i = 0; i < n; ++ i){
		assert(1 <= m[i]);
		long long r1 = r[i] % m[i], m1 = m[i];
		if(r1 < 0) r1 += m[i];
		if(m0 < m1) swap(r0, r1), swap(m0, m1);
		if(m0 % m1 == 0){
			if(r0 % m1 != r1) return {0, 0};
			continue;
		}
		long long g, im;
		tie(g, im) = inv_gcd(m0, m1);
		long long u1 = (m1 / g);
		if ((r1 - r0) % g) return {0, 0};
		long long x = (r1 - r0) / g % u1 * im % u1;
		r0 += x * m0, m0 *= u1;  // -> lcm(m0, m1)
		if(r0 < 0) r0 += m0;
	}
	return {r0, m0};
}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(ios::badbit | ios::failbit);
	auto __solve_tc = [&](int __tc_num){
		long long x, y, p, q;
		cin >> x >> y >> p >> q;
		long long res = numeric_limits<long long>::max();
		for(auto r0 = x; r0 < x + y; ++ r0){
			for(auto r1 = p; r1 < p + q; ++ r1){
				auto [t, lcm] = chinese_remainder_theorem({r0, r1}, {2 * x + 2 * y, p + q});
				if(lcm){
					ctmin(res, t);
				}
			}
		}
		if(res == numeric_limits<long long>::max()){
			cout << "infinity\n";
		}
		else{
			cout << res << "\n";
		}
		return 0;
	};
	int __tc_cnt;
	cin >> __tc_cnt;
	for(auto __tc_num = 0; __tc_num < __tc_cnt; ++ __tc_num){
		__solve_tc(__tc_num);
	}
	return 0;
}

/*

*/

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                    //
//                                   Coded by Aeren                                   //
//                                                                                    //
////////////////////////////////////////////////////////////////////////////////////////
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <string>
#include <cassert>

using namespace std;

typedef long long llint;

const int INF = 0x3f3f3f3f;
const llint INFLL = 0x3f3f3f3f3f3f3f3fLL;

void print() { cout << "\n"; }

template <typename...T, typename X>
void print(X&& x, T... args) { cout << x << " "; print(args...); }
int input() { return 0; }

template <typename...T, typename X>
int input(X& x, T&... args) {
    if (!(cin >> x)) return 0;
    return input(args...) + 1;
}

llint exgcd(llint a, llint b, llint& x, llint& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    llint r = exgcd(b, a%b, x, y);
    llint t = y;
    y = x - (a / b) * y;
    x = t;
    return r;
}

llint fast_mul(llint a, llint b, llint p){
    llint res = 0;
    while (b) {
        if (b & 1) {
            res = (res + a) % p;
        }
        b >>= 1;
        a = (a + a) % p;
    }
    return (res % p + p) % p;
}

llint excrt(const vector<llint>& a, const vector<llint>& b) {
    const int n = a.size();
    assert(a.size() == b.size());
    llint a1 = a[0];
    llint b1 = b[0];
    for (int i = 1; i < n; i++) {
         llint a2 = a[i];
         llint b2 = b[i];

         llint k1,k2;
         llint d = exgcd(a1, a2, k1, k2);

         if ((b2 - b1) % d != 0) {
             return -1;
         }
         llint t = a2 / d;
         k1 = (k1 % t + t) % t;
         k1 = fast_mul((b2 - b1) / d, k1, t);
         b1 = k1 * a1 + b1;
         a1 = a1 / d * a2;
    }
    return b1;
}


llint solve(int x, int y, int p, int q) {
    llint res = INFLL;
    for (int yy = 0; yy < y; yy++) {
        for (int qq = 0; qq < q; qq++) {
            llint m1 = 2LL * x + 2LL * y;
            llint r1 = 1LL * x + yy;

            llint m2 = 1LL * p + q;
            llint r2 = 1LL * p + qq;

            llint cur = excrt({m1, m2}, {r1, r2});
            if (cur == -1) {
                continue;
            }
            res = min(res, cur);
        }
    }
    if (res >= INFLL) {
        return -1;
    }
    return res;
}

int main() {
    int T;
    input(T);
    while (T--) {
        int x, y, p, q;
        input(x, y, p, q);

        llint res = solve(x, y, p, q);

        if (res == -1) {
            puts("infinity");
        } else {
            printf("%lld\n", res);
        }
    }

    return 0;
}

/*

^^^TEST^^^
3
5 2 7 6
1 1 3 1
999999999 1 1000000000 1
-----
20
infinity
1000000000999999999
$$$TEST$$$

*/
