#include <bits/stdc++.h>

using ull = unsigned long long int;
using ll = long long;
using ld = long double;
using pii = std::pair<int,int>;
using pll = std::pair<ll, ll>;
using vi = std::vector<int> ;
using vvi = std::vector<vi> ;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vd = std::vector<double> ;
using vvd = std::vector<vd> ;
using qi = std::queue<int> ;
using vpii = std::vector<std::pair<int, int> >;
using vpll = std::vector<pll>;
using namespace std;

#define rep(i,j) for(int (i)=0;(i)<(j);(i)++)
#define drep(i,j) for(int (i)=(j);(i) >= 0;(i)--)


template<class T1, class T2> inline void chmin(T1 &a, T2 b){if(a > b) a = b;}
template<class T1, class T2> inline void chmax(T1 &a, T2 b){if(a < b) a = b;}
template<class T> inline void pri(T a){cout << a << endl;}
template<class Z> using vec = vector<Z>;
template<class T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

inline void IN(void){
  return;
}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
  cin >> first;
  IN(rest...);
  return;
}

inline void OUT(void){
 cout << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest){
cout << first << " " ; 
OUT(rest...);
   return;
}
const int max_n = 3 * (1e5) + 1;
const int max_m = 83 * (1e5) + 1;
const ll INF = 8000000000000000000;
int n,m,k;
ll N;
int h,w;
string S;
int a,b,c;
vi v;
int ans;
double x,y,z;
vector<int> G[1010101];
int score[1010101];
pair<long,long> Extended_Euclid(ll a, ll b)
{
  if(b == 0)return {1,0};
  pair<long,long> e = Extended_Euclid(b,a%b);
  return {e.second,e.first - a/b * e.second};
}

long long int get_mod(ll a, ll mod)
{
  return (a%mod + mod)%mod;
}

pair<long long int, long long int> calc(long long int b1, long long int mod1, long long int b2, long long int mod2)
{
  pair<long long int,long long int> e = Extended_Euclid(mod1,mod2);
  long long int d = e.first * mod1 + e.second * mod2;
  if( (b1-b2)%d != 0)return {-1,0};
  long long int lcm = mod1 * mod2 / d;
  long long tmp = (b2 - b1)/d * e.first % (mod2/d);
  long long x = get_mod(b1 + mod1 * tmp, lcm);
  return {x,lcm};
}
 pair<long long int, long long  int> CRT(vector<long long int> &b, vector<long long int> &mod)
{
  pair<long long int,long long int> ans = {0,1};
  int N = b.size();
  for(int i=0;i<N;i++)
  {
    ans = calc(ans.first,b[i],ans.second, mod[i]);
  }
  return ans;
}

void solve()
{
  ll X,Y,P,Q; IN(X,Y,P,Q);
  ll ans = INF;
  for(ll t1 = X;t1 < X+Y; t1++)for(ll t2 = P;t2 < P+Q;t2++)
  {
   auto e = calc(t1,2*(X+Y),t2,P+Q) ;
   if(e.first > 0)chmin(ans,e.first);
  }
  if(ans == INF)
  {
    cout << "infinity" << endl;
    return ;
  }
  cout << ans << endl;
}

signed main (int argc, char* argv[]) {
   cin.tie(0);
   ios::sync_with_stdio(false);
   int cases=1;
   IN(cases);
   while(cases--)solve();
   //pri(ans);
   //for(auto c : ans){cout << c << endl;}
   //cout << fixed << setprecision(15) << ans << endl;
   return 0;
}
#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 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 solve(ll a,ll b,ll c)
{
    if(!c) return 0;
    ll g=__gcd(a,b);
    if(c%g) return -1;
    ll x,y;
    exgcd(a,b,x,y);
    x*=(c/g),y*=(c/g);
    return pos_mod(x,b/g);
}
int main()
{
    int T=qi();
    while(T--)
    {
         ll x=ql(),y=ql(),p=ql(),q=ql();
         ll ans=infl;
         for(ll i=p;i<=p+q-1;++i){
            ll res=solve(2*(x+y),p+q,i-x);
            if(res==-1) continue;
            gmn(ans,res*2*(x+y)+x);
         }
         for(ll i=x;i<=x+y-1;++i){
             ll res=solve(p+q,2*(x+y),i-p);
             if(res==-1) continue;
             gmn(ans,res*(p+q)+p);
          }
         if(ans==infl) puts("infinity");
         else pl(ans),pe();
    }
    return 0;
}