#include <bits/stdc++.h>

using namespace std;

#define int long long

int euclidExt(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int d = euclidExt(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

void shiftAnswer(int &x, int &y, int k, int a, int b, int g) {
    x += k * (b / g);
    y -= k * (a / g);
}

bool solve(int a, int b, int c, int &n, int &m, int minize) {
    int g = euclidExt(abs(a), abs(b), n, m);
    if (c % g)
        return false;
    n *= (c / g);
    m *= (c / g);
    if (a < 0)
        n *= -1;
    if (b < 0)
        m *= -1;
    if (minize == 0) {
        int k;
        int num = -n * g;
        if (num >= 0) {
            k = (num + abs(b) - 1) / abs(b);
        } else {
            k = num / abs(b);
        }
        shiftAnswer(n, m, k, abs(a), abs(b), g);

    } else {
        int k;
        int num = -m * g;
        if (num >= 0) {
            k = (num + abs(a) - 1) / abs(a);
        } else {
            k = num / abs(a);
        }
        shiftAnswer(n, m, -k, a, b, g);
    }
    return n >= 0 && m >= 0;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        int x, y, p, q;
        cin >> x >> y >> p >> q;

        int ans = LLONG_MAX;

        for (int k = 0; k < q; k++) {
            int n1 = 0, m1 = 0;
            if (solve(2 * x + 2 * y, -(p + q), p + k - x, n1, m1, 1))
                ans = min(ans, (p + q) * m1 + p + k);
        }

        for (int k = 0; k < y; k++) {
            int n2 = 0, m2 = 0;
            if (solve(2 * x + 2 * y, -(p + q), p - k - x, n2, m2, 1))
                ans = min(ans, (p + q) * m2 + p);
        }


        cout << (ans == LLONG_MAX ? "infinity" : to_string(ans)) << '\n';
    }
}



#include <bits/stdc++.h>
using namespace std;
template<typename T>
void read(T&x){
	x=0;
	int f=1;
	char ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-')f*=-1;
		ch=getchar();
	}
	while(isdigit(ch)){
		x=x*10+(ch-'0');
		ch=getchar();
	}x*=f;
}
template<typename T>
void write(T x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
//=============================================
typedef long long ll;
#define int ll
int t,x,y,p,q;
vector<int>T1,T2;
int mod[3];
int ans[3];
void prelude(){
	for(int i=x;i<x+y;++i)T1.push_back(i);
	for(int i=p;i<p+q;++i)T2.push_back(i);
}

int mul(int a,int b,int mod){
	int res=0;
	while(b){
		if(b&1)res=(res+a)%mod;
		a=(a+a)%mod;
		b>>=1;
	}
	return res;
}
int exgcd(int a,int b,int&x,int&y){
	if(b==0){
		x=1,y=0;
		return a;
	}
	int r=exgcd(b,a%b,x,y);
	int t=x;
	x=y;
	y=t-a/b*y;
	return r;
}

int excrt(int ans[],int mod[],int n){
	int M=mod[0];
	int res=ans[0];
	for(int i=1;i<n;++i){
		int a=M,b=mod[i],c=((ans[i]-res)%b+b)%b;
		int x,y;
		int r=exgcd(a,b,x,y);
		if(c%r)return -1;
		int bb=b/r;
		x=mul(x,c/r,bb);
		res+=x*M;
		M*=bb;
		res=(res%M+M)%M;
	}
	return (res%M+M)%M;
}

int solve(){
	ll res=INT64_MAX;
	mod[0]=2*x+2*y;
	mod[1]=p+q;
	T1.clear();
	T2.clear();
	prelude();
	for(auto t1:T1){
		for(auto t2:T2){
			ans[0]=t1;ans[1]=t2;
			int tmp=excrt(ans,mod,2);
			if(tmp==-1)continue;
			res=min(res,tmp);
		}
	}
	return res==INT64_MAX?-1:res;
}

signed main(){
	//freopen("in.txt","r",stdin);
	read(t);
	while(t--){
		read(x),read(y),read(p),read(q);
		int res=solve();
		if(res==-1)puts("infinity");
		else write(res),putchar('\n');
	}
	return 0;
}