#include<bits/stdc++.h> 
using namespace std;
namespace Sakurajima_Mai{
    #define ms(a) memset(a,0,sizeof(a))
    #define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
    #define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
    #define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
    #define ce(i,r) i==r?'\n':' '
    #define pb push_back 
    #define all(x) x.begin(),x.end()
    #define gmn(a,b) a=min(a,b)
    #define gmx(a,b) a=max(a,b)
    #define fi first
    #define se second                         
    typedef long long ll;
    typedef unsigned long long ull;
    typedef double db;
    const int infi=2e9;//infi??,????inf????int
    const ll infl=4e18;
    inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
    inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
    //std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Sakurajima_Mai;
namespace Fast_Read{
    inline int qi(){
        int f=0,fu=1; char c=getchar();
        while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
        while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
        return f*fu;
    }
    inline ll ql(){
        ll f=0;int fu=1; char c=getchar();
        while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
        while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
        return f*fu;
    }
    inline db qd(){
        char c=getchar();int flag=1;double ans=0;
        while((!(c>='0'&&c<='9'))&&c!='-') c=getchar();
        if(c=='-') flag=-1,c=getchar();
        while(c>='0'&&c<='9') ans=ans*10+(c^48),c=getchar();
        if(c=='.'){c=getchar();for(int Bit=10;c>='0'&&c<='9';Bit=(Bit<<3)+(Bit<<1)) ans+=(double)(c^48)*1.0/Bit,c=getchar();}
        return ans*flag;
    }
}
namespace Read{
    #define si(a) scanf("%d",&a)
    #define sl(a) scanf("%lld",&a)
    #define sd(a) scanf("%lf",&a)
    #define ss(a) scanf("%s",a)
    #define rai(x,a,b) repi(i,a,b) x[i]=qi()
    #define ral(x,a,b) repi(i,a,b) x[i]=ql()
}
namespace Out{
    #define pi(x) printf("%d",x)
    #define pl(x) printf("%lld",x)
    #define ps(x) printf("%s",x)
    #define pc(x) printf("%c",x)
    #define pe() puts("")
}
namespace DeBug{
    #define MARK false
    #define DB if(MARK) 
    #define pr(x) cout<<#x<<": "<<x<<endl
    #define pra(x,a,b) cout<<#x<<": "<<endl; \
            repi(i,a,b) cout<<x[i]<<" ";     \
            puts("");   
    #define FR(a) freopen(a,"r",stdin)
    #define FO(a) freopen(a,"w",stdout)
}
using namespace Fast_Read;
using namespace Read;
using namespace Out;
using namespace DeBug;
ll a[5],m[5];
ll exgcd(ll a,ll b,ll &x,ll &y)  
{
    if(!a&&!b)  return -1; 
    if(!b){ x=1,y=0; return a; }
    ll d=exgcd(b,a%b,y,x); 
    y-=a/b*x;
    return d;
}
ll qmul(ll a,ll b,ll mod)
{
    ll res=0; a%=mod,b%=mod; 
    while(b){
        if(b&1){ res=(res+a)%mod; }
        b>>=1,a=(a+a)%mod; 
    }
    return res;
}
ll excrt(ll *a,ll *m,int n)// 模m余a 注意所有m的lcm会不会爆ll
{
    ll x,y,M=m[1],ans=a[1];
    repi(i,2,n){
        ll ta=M,tb=m[i],c=(a[i]-ans%tb+tb)%tb;
        ll d=exgcd(ta,tb,x,y),bg=tb/d;
        if(c%d!=0) return infl; 
        x=qmul(x,c/d,bg);
        ans+=x*M,M*=bg,ans=(ans%M+M)%M; 
    } 
    return (ans%M+M)%M;//最小非负解
}
int main()
{
    int T=qi();
    while(T--)
    {
         ll x=ql(),y=ql(),p=ql(),q=ql();
         ll ans=infl;
         m[1]=2*(x+y),m[2]=p+q;
         repi(i,x,x+y-1)repi(j,p,p+q-1) a[1]=i,a[2]=j,gmn(ans,excrt(a,m,2));
         if(ans==infl) puts("infinity");
         else pl(ans),pe();
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for(int i = (int)(s); i < (int)(n); ++i)

ll INF = 1ll << 60;

//拡張ユークリッドの互除法
//ap+bq = gcd(a,b)となるp,qを求め、gcd(a,b)を返す
ll extGCD(ll a, ll b, ll &p, ll &q){
  if(b == 0){
    p = 1;
    q = 0;
    return a;
  }
  
  ll d = extGCD(b, a%b, q, p);
  q -= a/b*p;
  
  return d;
  
}

//中国剰余定理
//x≡b1(mod m1),x≡b2(mod m2)を満たすx≡r(mod lcm(m1,m2))のr, lcmを返す
pll crt(ll b1, ll b2, ll m1, ll m2){
  ll p, q;
  ll d = extGCD/*拡張ユークリッドの互除法*/(m1, m2, p, q);
  if((b2-b1)%d != 0)
    return make_pair(0, -1);
  ll m = m1*m2/d;
  ll tmp = (b2-b1)/d*p%(m2/d);
  ll r = ((b1+m1*tmp)%m+m)%m;
  return make_pair(r, m);
}

int main(){
  int t;
  cin >> t;
  
  rep(i, 0, t){
    ll ans = INF;
    ll x, y, p, q;
    cin >> x >> y >> p >> q;
    rep(t1, x, x+y){
      rep(t2, p, p+q){
        auto [r, lcm] = crt(t1, t2, 2*(x+y), p+q);
        if(lcm == -1)
          continue;
        if(ans > r)
          ans = r;
      }
    }
    if(ans == INF)
      cout << "infinity" << endl;
    else
      cout << ans << endl;
    
  }
  
  return 0;
  
}