#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 1000000007LL
//#define MOD 998244353LL
#define doublecout(a) cout<<setprecision(16)<<a<<endl;
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
using vvvi = vector<vvi>; // intの2次元の型に vvi という別名をつける
const ll llMAX=9223372036854775807LL; 
const ll llMIN=-9223372036854775808LL;

void myprint1D(vi &data)
{
  REP(i,data.size())
    cout<<data[i]<<" ";
  cout<<endl;
}

//配列を[y][x]で表示
void myprint2D_T(vvi &data)
{
  REP(i,data.size())
    myprint1D(data[i]);
}

//配列を[x][y]で表示
void myprint2D(vvi &data)
{
  ll l1=data.size();
  ll l2=data[0].size();
  REP(j,l2){
    REP(i,l1)
      cout<<data[i][j]<<" ";
    cout<<endl;
  }
}

//print(a,b...)って使い方
void print1(ll a){cout<<a<<endl;}
void print2(ll a,ll b){cout<<a<<" "<<b<<endl;}
void print3(ll a,ll b,ll c){cout<<a<<" "<<b<<" "<<c<<endl;}
void print4(ll a,ll b,ll c,ll d){cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;}
void print5(ll a,ll b,ll c,ll d,ll e){cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;}

//moddivとnCkとgarner
//c剰余下でaをbで割った値
ll moddiv(ll a,ll b,ll c){
	ll x0=c,x1=b,x2,n0=0LL,n1=1LL,n2,t=a%b,m,ans;
	if (t==0LL) return a/b;
		for(int i=0;i<900;i++){
      m=x0/x1;
      x2=x0-x1*m;
      n2=(n0-m*n1)%c;
      if (x2==1LL){
        ans=(n2+c)%c;
        break;
      }
      x0=x1;x1=x2;
      n0=n1;n1=n2;
    }
	return (a+((t*ans)%c)*b-t)/b;
}
//剰余の割ざん

//aのn乗 mod c
ll expmod(ll a,ll n,ll c){
  ll ans=1LL,aa=a,beki=n;
  for(int i=0;i<64;i++){
    if (beki%2==1LL) ans=ans*aa%c;
    aa=aa*aa%c;
    beki/=2LL;
    if (beki==0LL)break;
  }
  return ans;
}

//nCk
vi mulmod(500001);//0!,1!,2!,3!,4!,,,,
void first_nCk(){
  mulmod[0]=1LL;
  for(ll i=1;i<500001;i++){
    mulmod[i]=mulmod[i-1]*i%MOD;
  }
}
ll nCk(ll nn,ll kk){
  return moddiv(mulmod[nn],mulmod[kk]*mulmod[nn-kk]%MOD,MOD);
}
ll modinv(ll a,ll b){
  return moddiv(1,a%b,b);
}
//条件gcd(a,b)==1,ar<a,br<b
ll garner(__int128_t a,__int128_t b,__int128_t ar,__int128_t br){
  __int128_t tmp=(br-ar)%b*modinv(a,b)%b;
  if (tmp<0)tmp+=b;
  return ar+tmp*a;
}
ll gcd(ll a,ll b){
  ll r = a % b;
  while(r!=0LL){
    a = b;
    b = r;
    r = a % b;
  }
return b;
}


ll t,x,y,p,q;

int main(){
  ll ans=0;
  ll t=0;
  cin>>t;
  for(ll ttt=0;ttt<t;ttt++){
    cin >> x >> y>>p>>q;
    ll xy=x*2+y*2;
    ll pq=p+q;
    ans=llMAX;

/*
    ll t=gcd(max(x-p,p-x),gcd(xy,pq));
        if (t>=y+q){
          cout<<"infinity"<<endl;
          continue;
        }
        */

    for(ll i=0;i<y;i++){
      for(ll j=0;j<q;j++){
        ll tttt=max(x+i-(p+j),(p+j)-(x+i));
        ll gd=gcd(xy,pq);
        if (tttt%gd!=0)continue;
        
        if (gd!=1){

          if (x+i-(p+j)>0){
            ll g;
            if ((xy/gd==1)|(pq/gd==1)){
              g=(x+i-(p+j))/gd;
            }else{
              g=garner(xy/gd,pq/gd,(x+i-(p+j))/gd,0);
            }
            g=g*gd;
            g+=(p+j);
            ans=min(ans,g);
          }else{
            ll g;
            if ((xy/gd==1)|(pq/gd==1)){
              g=((p+j)-(x+i))/gd;
            }else{
              g=garner(xy/gd,pq/gd,0,((p+j)-(x+i))/gd);
            }
            g=g*gd;
            g+=(x+i);
            ans=min(ans,g);
          }

        }else{

          ll g=garner(xy,pq,x+i,p+j);
          ans=min(ans,g);
        }
        
      }
    }

    if (ans==llMAX){
      
          cout<<"infinity"<<endl;
          continue;
    }
    cout<<ans<<endl;
  }
  return 0;
}

/*
298 2 98 102
*/
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define IFOR(i, a, b) for (long long i = (a); i <= (long long)(b); i++)
using namespace std;
using VL = vector<long long>;
using ll = int64_t;
ll mod(ll x,ll m){ll y=x%m;return (y>=0LL)?y:y+m;}
template<class T>bool chmin(T& a, T b){if(a>b){a=b;return true;}else return false;}
ll gcd(ll a,ll b,ll& x,ll& y)
{if(a){ll d=gcd(b%a,a,x,y);y-=(b/a)*x;swap(x,y);if(d<0LL){d=-d;x=-x;y=-y;}return d;}else{x=0;y=1;return b;}}

int main(){
    int T;
    cin>>T;
    string ans = "";
    REP(i,T){
        ll t = LLONG_MAX;
        ll X,Y,P,Q;
        scanf("%lld %lld %lld %lld",&X,&Y,&P,&Q);
        ll a=(X+Y)<<1, b=P+Q;
        ll x, y;
        ll d = gcd(a, -b, x, y);
        ll u = b/d, v = a/d;
        ll stop0=P-X, stop1=P-X+Q-1LL;
        ll wake0=P-X-Y+1LL, wake1=P-X;
        ll from=min(stop0, wake0), to=max(stop1, wake1);
        IFOR(z, from, to){
            if(mod(z,d)!=0LL)continue;
            if(stop0<=z && z<=stop1){
                chmin(t,a*mod(z/d*x,u)+X);
            }else{
                chmin(t, b*mod(z/d*y,v)+P);
            }
        }
        ans += (t==LLONG_MAX ? "infinity" : (to_string(t)))+"\n";
    }

    cout << ans;

    return 0;
}