#include<iostream>
#define ll long long
using namespace std;
ll T,X,Y,P,Q,a,b,c,k1,k2,tans;
ll exgcd(ll a,ll b,ll &x,ll &y) {
	if(!b) {
		x=1,y=0;
		return a;
	}
	int t=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return t;
}
int main() {
	cin>>T;
	while(T--) {
		cin>>X>>Y>>P>>Q;
		a=2*X+2*Y,b=P+Q;
		tans=5e18;
		for(int q=0; q<Q; q++) {
			for(int y=0; y<Y; y++) {
				c=P+q-X-y;
				int t=exgcd(a,b,k1,k2);
				if(c%t)continue;

				k1=k1*c/t;
				k1=(k1%(b/t)+(b/t))%(b/t);
				tans=min(tans,a*k1+y+X);
			}
		}
		if(tans==5e18)cout<<"infinity\n";
		else cout<<tans<<endl;
	}
}
//#include <atcoder/maxflow.hpp>
#include <memory>
#include <iostream>
#include <map>
#include <list>
#include <set>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <fstream>
#include <iterator>
#include <random>
#include <chrono>
#include <complex>
#include <thread>
#include <bitset>
#include <assert.h>

#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s

typedef long long ll;

using namespace std;
//using namespace atcoder;

ll modd = (1000LL * 1000LL * 1000LL + 7LL);
//ll modd = 998244353;

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

bool find_any_solution(ll a, ll b, ll c, ll &x0, ll &y0, ll &g) {
    g = gcd(abs(a), abs(b), x0, y0);
    if (c % g) {
        return false;
    }

    x0 *= c / g;
    y0 *= c / g;
    if (a < 0) x0 = -x0;
    if (b < 0) y0 = -y0;
    return true;
}


int main(int argc, char *argv[])   {

//    ios_base::sync_with_stdio(false);
//    cin.tie(NULL);

    cout.precision(17);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> rand_gen(0, modd);   // rand_gen(rng) gets the rand no

    uniform_real_distribution<double> ff(-1,1);

//    auto start = chrono::steady_clock::now();

    readint(test_cases);
//    int test_cases = 1;
    forr(t, 1, test_cases) {
        readll(x); readll(y); readll(p); readll(q);

        ll per_train = 2*x+2*y, per_sleep = p+q;

        set<ll> solutions;
        forr(i,0,y) {
            forr(j,0,q) {
                ll a = per_train, b = -per_sleep, c = p+j-x-i, x0,y0,g;
                if (find_any_solution(a,b,c,x0,y0,g)) {
                    ll x_shift = -b/g, y_shift = a/g;
                    if (x0<0) {
                        ll k = abs(x0)/x_shift+4;
                        x0 += k * x_shift;
                        y0 += k * y_shift;
                    }
                    ll k = x0/x_shift;
                    x0 -= k * x_shift;
                    y0 -= k * y_shift;
                    solutions.insert(p+j+y0*per_sleep);
                }
            }
        }
        if (solutions.empty()) {
            cout << "infinity" << endl;
        } else {
            cout << *solutions.begin() << endl;
        }

    }

//        auto stop = chrono::steady_clock::now(); 
//        auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
//        cout << "Duration: " << duration.count() << endl;
    return 0;
}

