//Made by imalyd
//Problem Name: ...
//Problem Link: ...
//Category: ...
#include<bits/stdc++.h>
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define rep(i,a,b) for(i=(a);i<=(b);++i)
#define per(i,a,b) for(i=(a);i>=(b);--i)
#define REP(i,a,b) for(i=(a);i< (b);++i)
#define PER(i,a,b) for(i=(a);i> (b);--i)
#define ERR(...) fprintf(stderr,__VA_ARGS__)
template<class T>inline void cmn(T &x,const T &a){if(a<x)x=a;}
template<class T>inline void cmx(T &x,const T &a){if(x<a)x=a;}
template<class T>inline bool bmn(T &x,const T &a){if(a<x){x=a;return true;}else return false;}
template<class T>inline bool bmx(T &x,const T &a){if(x<a){x=a;return true;}else return false;}
const int p=998244353,ps2=p-2;
inline void add(int &x){if(x>=p)x-=p;}
inline void sub(int &x){if(x< 0)x+=p;}
#define MUL(a,b) ((0ll|(a))*(b)%p)
template<class T>int expo(int a,T b){
	int s(1);
	while(b){if(b&1)s=MUL(s,a);b>>=1;a=MUL(a,a);}
	return s;
}
int expo(int a){
	int b(ps2),s(1);
	while(b){if(b&1)s=MUL(s,a);b>>=1;a=MUL(a,a);}
	return s;
}

inline ll gcd(ll a,ll b){
	return b?gcd(b,a%b):a;
}
int S[150];
inline void sol(ll n,ll m){
	int t=0;
	for(;;){
		if(!n){while(m--)S[++t]=2;break;}
		if(!m){while(n--)S[++t]=1;break;}
		if(n>m){S[++t]=3;n-=m;}else{S[++t]=4;m-=n;}
		if(t>130)return;
	}
	if(t>130)return;
	printf("%d\n",t);
	while(t)printf("%d\n",S[t--]);
	exit(0);
}
int main(){
#ifdef ONLINE_JUDGE
	//freopen("problemname.in","r",stdin);
	//freopen("problemname.out","w",stdout);
#endif
	ll n,m;int t=0;
	scanf("%lld",&n);
	m=(sqrt(1.25)-0.5)*n;
	cmx(m,1ll);
	cmn(m,n-1ll);
	ll i;
	per(i,m,1ll)if(gcd(n,i)==1ll)sol(n,i);
	rep(i,m,n  )if(gcd(n,i)==1ll)sol(n,i);
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define int long long
int exgcd(int a,int b,int &x,int &y){
	if(!b){
		x=1;
		y=0;
		return a;
	}
	else {
		int d=exgcd(b,a%b,x,y);
		int temp=x;
		x=y;
		y=temp-a/b*y;
		return d;
	}
}
//求ax+by=c,的x的最小整数解，无解为-1
int findK(int a,int b,int c){
    int x,y;
    int gcd=exgcd(a,b,x,y);
    if(c%gcd)return -1;
    int k=c/gcd;
    int p=x*k;
    int q=p*k;
    p%=b/gcd;
    p+=b/gcd;
    p%=b/gcd;
    return p;
}
int T;
int x,y,p,q;
void solve(){
    cin>>T;
    while(T--){
        cin>>x>>y>>p>>q;
        int a,b,c,d,d1,d2;
        a=x,b=x+y-1,c=p,d=p+q-1,d1=2*(x+y),d2=p+q;
        int ans=2e18;
        if((a>=c&&a<=d)||(c>=a&&c<=b)){
            if(a>=c&&a<=d) ans=min(ans,a);
            if(c>=a&&c<=b) ans=min(ans,c);
            cout<<ans<<endl;
        }
        else{
            if(d1==d2) cout<<"infinity"<<endl;
            else{
                int num=x%d2;
                for(int i=c;i<=d;++i){
                    int a1=a,b1=d1,c1=d2,d11=i;
                    int num1=findK(b1,c1,d11-a1);
                    if(findK(b1,c1,d11-a1)!=-1){
                        ans=min(ans,num1*b1+a1);
                    }
                }
                num=p%d1;
                for(int i=a;i<=b;++i){
                    int a1=c,b1=d2,c1=d1,d11=i;
                    int num1=findK(b1,c1,d11-a1);
                    if(findK(b1,c1,d11-a1)!=-1){
                        ans=min(ans,num1*b1+a1);
                    }
                }
                if(ans==2e18)  cout<<"infinity"<<endl;
                else cout<<ans<<endl;
            }
        }

    }
}
signed main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    solve();
    return 0;
}
