#include<bits/stdc++.h>
using namespace std;
int t[10];
int tp[50]={8,16,24,32,40,48,56,64,72,80,88,96};
int main()
{
	string s;
	cin>>s;
	for(int i=0;i<s.size();i++)
	{
		int d=s[i]-'0';
		t[d]++;
	}
	if(s.size()>=3)
	{
	for(int i=112;i<=999;i+=8)
	{
		int a1,b1,c1;
		a1=i/100;
		b1=i/10%10;
		c1=i%10;
		t[a1]--;
		t[b1]--;
		t[c1]--;
		if(t[a1]>=0&&t[b1]>=0&&t[c1]>=0)
		{
			cout<<"Yes";
			return 0;
		}
		else
		{
		t[a1]++;
		t[b1]++;
		t[c1]++;
		}
	}
	cout<<"No";
	return 0;
	}
	if(s.size()==2)
	{
	    for(int i=0;i<=96;i+=8)
    	{
    		int a1,b1;
	    	a1=i/10;
    		b1=i%10;
    		t[a1]--;
    		t[b1]--;
    		if(t[a1]>=0&&t[b1]>=0)
    		{
    			cout<<"Yes";
    			return 0;
    		}
    		else
    		{
	        	t[a1]++;
	        	t[b1]++;
	    	}
	    }
	    cout<<"No";
	    return 0;
	}
	if(s.size()==1)
	{
		int a=s[0]-'0';
		if(a%8==0)
		cout<<"Yes";
		else
		cout<<"No";
	}
	return 0;
}
#include<bits/stdc++.h>
#define ll long long
#define re register
#define INF 2147483647
using namespace std;

inline ll read()
{
	ll f=1,x=0;char s=getchar();
	while(s<'0'||s>'9')
	{
		if(s=='-') f=-1;
		s=getchar();
	}
	while(s>='0'&&s<='9')
	{
		x=x*10+s-48;
		s=getchar();
	}
	return f*x;
}

ll mul(ll a,ll b,ll mod)
{
    ll res=0;
    while(b>0)
    {
        if(b&1) res=(res+a)%mod;
        a=(a+a)%mod;
        b>>=1;
    }
    return res;
}

ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0){x=1;y=0;return a;}
    ll gcd=exgcd(b,a%b,x,y);
    ll tp=x;
    x=y; y=tp-a/b*y;
    return gcd;
}
int n=2;
ll ai[20],bi[20];

ll excrt()
{
    ll x,y,k;
    ll M=bi[1],ans=ai[1];
    for(int i=2;i<=n;i++)
    {
        ll a=M,b=bi[i],c=(ai[i]-ans%b+b)%b;
        ll gcd=exgcd(a,b,x,y),bg=b/gcd;
        if(c%gcd!=0) return -1;
        x=mul(x,c/gcd,bg);
        ans+=x*M;
        M*=bg;
        ans=(ans%M+M)%M;
    }
    return (ans%M+M)%M;
}

const ll LLINF=1ll<<62; 
int main()
{
	int T=read();
	while(T--)
	{
		int x=read(),y=read(),p=read(),q=read();
		ll A=2*x+2*y,B=p+q;
		ll ans=LLINF;
		for(int i=0;i<y;i++)
			for(int j=0;j<q;j++)
			{
				ai[1]=i+x,bi[1]=A;
				ai[2]=j+p,bi[2]=B;
				ll res=excrt();
				if(res!=-1) ans=min(ans,res);
			}
		if(ans==LLINF) puts("infinity");
		else printf("%lld\n",ans);
	}
	return 0;
}