#include<bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define lowbit(x) (x&-x)
#define reg register
#define mkpr make_pair
#define fir first
#define sec second
typedef long long LL;
typedef unsigned long long uLL;
const LL INF=0x7f7f7f7f7f7f7f7f;
const int mo=998244353;
const int zero=500;
const LL jzm=2333;
const int iv2=499122177;
const double Pi=acos(-1.0);
typedef pair<int,int> pii;
const double PI=acos(-1.0);
template<typename _T>
_T Fabs(_T x){return x<0?-x:x;}
template<typename _T>
void read(_T &x){
	_T f=1;x=0;char s=getchar();
	while(s>'9'||s<'0'){if(s=='-')f=-1;s=getchar();}
	while('0'<=s&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=getchar();}
	x*=f;
}
template<typename _T>
void print(_T x){if(x<0){x=(~x)+1;putchar('-');}if(x>9)print(x/10);putchar(x%10+'0');}
int t;LL X,Y,P,Q,x[1005],y[1005],d[1005];
LL exgcd(LL a,LL b,LL &x,LL &y){
	if(!b){x=1;y=0;return a;}
	LL d=exgcd(b,a%b,y,x);y-=x*(a/b);return d;
}
signed main(){
	read(t);
	while(t--){
		read(X);read(Y);read(P);read(Q);
		LL A=P+Q,B=2ll*X+2ll*Y,res=INF;
		for(LL i=-Q;i<=Y;i++)d[i+zero]=exgcd(A,B,x[i+zero],y[i+zero]);
		for(LL i=P;i<P+Q;i++)
			for(LL j=X;j<X+Y;j++){
				LL t=j-i,d1=d[t+zero-X+P];if(t%d1!=0)continue;
				LL ax=t/d1*x[t+zero-X+P],ay=i/d1*y[t+zero-X+P],a1=B/d1,b1=A/d1,k;
				if(ax<0)k=(a1-ax)/a1,ax+=k*a1,ay-=k*b1;
				if(ax>=0)k=(ax-1)/a1,ax-=k*a1,ay+=k*b1;
				if(ay>0)k=(ay-1)/b1,ax+=k*a1,ay-=k*b1;
				res=min(ax%a1*A+i,res);
			}
		for(LL i=-Q;i<=Y;i++)d[i+zero]=x[i+zero]=y[i+zero]=0;
		if(res>INF-1)puts("infinity");else printf("%lld\n",res);
	}
	return 0;
}
/*
[(2X+2Y)n+X,(2X+2Y)n+X+Y)
[(P+Q)m+P,(P+Q)(m+1))
(X-P)<=(P+Q)m%(2X+2Y)<(X+Y-P)
(P+Q)x+(2X+2Y)y=+j-i
*/
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef complex<double> comp;
const ll INF=1e9+7;
const ll inf=INF*INF;
const ll MOD=1e9+7;
const ll mod=MOD;
const int MAX=200010;

int main(){
    ll n;cin>>n;
    map<ll,ll>ma;
    ll b=1LL;
    ll cnt=0;
    while(b*3LL<inf){
        b*=3LL;
        cnt++;
        ma[b]=cnt;
    }
    ll c=1LL;
    bool f=0;
    cnt=0;
    pll ans;
    while(c*5LL<=n){
        cnt++;
        c*=5LL;
        if(ma.find(n-c)!=ma.end()){
            ans.fi=ma[n-c];
            ans.se=cnt;
            f=1;
        }
        if(f)break;
    }
    if(f){
        cout<<ans.fi<<' '<<ans.se<<endl;
    }else{
        cout<<-1<<endl;
    }
}