#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define rrep(i,n) for(int i=int(n-1);i>=int(0);--i)
#define fore(i,a) for(auto &i:a)
#define all(x) x.begin(),x.end() 
#define sz(x) ((int)(x).size())
#define bp(x) (__builtin_popcount((long long)(x)))
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define V vector
#define P pair<int,int>
#define TP tuple<int,int,int>
#define F first
#define S second
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
using ll = long long;
using ld = long double;
using namespace std;
const int INF = 1001001001;
const ll INFL = 1001001001001001001;
const int MAX = 2e6+1;

int main() {
    ll a,b;cin>>a>>b;
    if(a>b)swap(a,b);
    ll ans=1;
    rep(i,2,b+1){
        if(ceil(1.0*a/i)*i+i<=b)ans=i;
    }

    cout<<ans<<endl;
    return 0;
}
#include<cstdio>//JZM YYDS!!!
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#define ll long long
#define MAXN 200005
#define uns unsigned
#define MOD 998244353ll
#define INF 0x7f7f7f7f
#pragma GCC optimize("O2")
using namespace std;
inline ll read(){
	ll x=0;bool f=1;char s=getchar();
	while((s<'0'||s>'9')&&s>0){if(s=='-')f^=1;s=getchar();}
	while(s>='0'&&s<='9')x=(x<<1)+(x<<3)+s-'0',s=getchar();
	return f?x:-x;
}
ll x,y,p,q;
inline ll exgcd(ll a,ll b,ll&x,ll&y){
	if(b==0){x=1,y=0;return a;}
	ll res=exgcd(b,a%b,y,x);
	y-=a/b*x;return res;
}
signed main()
{
	for(int T=read();T--;){
		x=read(),y=read(),p=read(),q=read();
		ll md=(x+y)<<1,lim=md*(p+q)+1,ans=lim;
		ll b=p,a=p+q,K,f,g=exgcd(p+q,md,K,f);
		for(ll i=x;i<x+y;i++){
			ll c=((i-b)%md+md)%md,k=K;
			if(c%g!=0)continue;
			c/=g,k=(k*c%md+md)%(md/g);
			if((k*a+b)%md>=x&&(k*a+b)%md<x+y)ans=min(ans,k*a+b);
		}
		md=p+q,b=x,a=(x+y)<<1,g=exgcd((x+y)<<1,md,K,f);
		for(ll i=p;i<p+q;i++){
			ll c=((i-b)%md+md)%md,k=K;
			if(c%g!=0)continue;
			c/=g,k=(k*c%md+md)%(md/g);
			if((k*a+b)%md>=p&&(k*a+b)%md<p+q)ans=min(ans,k*a+b);
		}
		if(ans>=lim)printf("infinity\n");
		else printf("%lld\n",ans);
	}
	return 0;
}