#include<bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define int long long
template<typename T>
void dbg_a(T *a,int l,int r){cout<<" {";for(int i = l;i<r;i++) cout<<a[i]<<", ";cout<<a[r]<<"}"<<endl;}
typedef long long ll;
template<typename T = int>
inline T read(){
    T f = 1,r = 0;
    char c = getchar();
    while(c<'0'||c>'9'){if(c == '-') f = -1; c = getchar();}
    while(c>='0'&&c<='9'){ r = (r<<1) + (r<<3) + c - '0';c = getchar();}
    return f*r;
}
typedef pair<int,int> PII;
#define fi first
#define se second
#define mst(a,b) memset(a,b,sizeof(a))
#define For(i,a,b) for(int i = a;i<=b;i++)
#define For_(i,a,b) for(int i = a;i>=b;i--)
#define _for(i,a) for(int i = 0;i<a;i++)
#define All(x) x.begin(),x.end()

//    For(i,1,n) For(j,1,m) printf("f[%lld][%lld] = %lld\n",i,j,f[i][j]);
 
const int N = 2e5 + 10,M = 2*N + 10,INF = 0x3f3f3f3f;
const int mod = 998244353;
int h[N],ne[M],e[M],idx;
void add(int a,int b){
    e[++idx] = b;ne[idx] = h[a];h[a] = idx;
}
int exgcd(int a,int b,int &x,int &y){
    int d = a;
    if(b){ d = exgcd(b, a%b, y, x);y -= (a/b)*x;}
    else{x = 1;y = 0;}
    return d;
}
int inv(int a,int mo){
    int x,y;
    int d = exgcd(a,mo,x,y);
    return d == 1 ? (x + mo)%mo : -1;//-1表示不存在，并对逆元调整
}
int gcd(int a,int b){
    return b?gcd(b,a%b):a;
}
bool merge(int a1,int m1,int a2,int m2,int &A,int &mo){
    int c = (a2 - a1),d = gcd(m1,m2);
    if(c % d) return 0;//gcd(m1,m2)|(a2 - a1)时才有解
    c = (c%m2 + m2)%m2;//将c变为[0,m2)之间的数
    c /= d;m1 /= d;m2 /= d;//两边除以d
    c = c*inv(m1,m2) % m2;//将m1/d 移到右边
    mo = m1*m2*d;// mo = m1 * m2 /d
    A = (c*m1%mo*d%mo + a1)%mo;
    return 1;
}
int EXCRT(int n,int a[],int mo[]){
    int a1 = a[1],m1 = mo[1],A,Mo;
    for(int i = 2;i<=n;i++){
        if(!merge(a1,m1,a[i],mo[i],A,Mo)) return -1;//不合法
        a1 = A;m1 = Mo;
    }
    return (a1 + m1)%m1;
}
int n;
void solve(){
    int x,y,p,q;
    cin>>x>>y>>p>>q;
    int a[3] = {},mo[3] = {};
    int ans = 3e18;
    For(i,x,x+y-1){
        For(j,p,p+q-1){
            a[1] = i,mo[1] = 2*(x+y);
            a[2] = j,mo[2] = p + q;
            int res = EXCRT(2,a,mo);
            if(res == -1) continue;
            ans = min(res,ans);
        }
    }
    if(ans == 3e18)cout<<"infinity"<<"\n";
    else cout<<ans<<"\n";
}
signed main(){
    int T = 1;
    T = read();
    while(T--) solve();
    system("pause");
    return 0;
}

#include <iostream>
#include <string>
#include <map>
#include <climits>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;

// a*x - b*y = return value = GCD(a,b). x,y >= 0
ll egcd(ll a, ll b, ll& x, ll& y) { // MINUS B*Y
	if (b == 0) {
		x = 1; y = 0;
		return a;
	}

	ll d = egcd(b, a % b, y, x);
	y = a - x * (a / b) - y;
	x = b - x;
	return d;
}
// modular inverse; works for any mod p (coprime with a)
ll inverse(ll a, ll p) {
	ll x, y;
	egcd(a % p + p, p, x, y);
	return x % p;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// solves the system of congruences:
// x = a (mod m)
// x = b (mod n)
// returns res such that x = res (mod lcm(m,n))
// no such res => return -1
ll CRT(ll a, ll m, ll b, ll n) {
	b = (b + n - (a % n)) % n;
	ll d = gcd(m, n);
	if (b % d != 0) return -1;

	ll oldM = m;
	m /= d; b /= d; n /= d;
	return ((b * inverse(m, n)) % n) * oldM + a;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int t; cin >> t;
	while (t--) {
		ll X, Y, P, Q; cin >> X >> Y >> P >> Q;
		ll ans = LLONG_MAX;
		for (int i = X; i < X + Y; i++) {
			for (int j = P; j < P + Q; j++) {
				ll curT = CRT(i, 2 * (X + Y), j, P+Q);
				if (curT == -1) { continue; }
				ans = min(ans, curT);
			}
		}
		if (ans == LLONG_MAX) { cout << "infinity" << '\n'; }
		else { cout << ans << '\n'; }
	}
}