#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <cmath>
#include <functional>
#include <cassert>
#include <iomanip>
#include <numeric>
#include <memory>
#include <random>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d, e) vector<vector<vector<e>>>(a, VV(b, c, d, e));
#define all(obj) (obj).begin(), (obj).end()
typedef long long ll;
typedef long double ld;
typedef std::vector<ll> v1;
typedef std::vector<v1> v2;
typedef std::vector<v2> v3;
using namespace std;


long long floor_sum(long long n,long long m,long long a,long long b){
	long long res=0;
	if(a>=m){
		res+=n*(n-1)/2*(a/m);
		a%=m;
	}
	if(b>=m){
		res+=n*(b/m);
		b%=m;
	}

	long long y_max=(a*(n-1)+b)/m;
	if(y_max==0) return res;
	return res+y_max+floor_sum(y_max,a,m,(a*(n-1)+b)-m*y_max);
}

void solve(){
  ll x, y, p, q;
  scanf("%lld %lld %lld %lld", &x, &y, &p, &q);

  ll l = 0, r = 1000000000;
  if(floor_sum(r, p+q, 2*(x+y), x+y+q-1) == floor_sum(r, p+q, 2*(x+y), x)){
    std::cout << "infinity" << '\n';
    return;
  }

  while(r-l>1){
    ll mid = (l+r)/2;
    if(floor_sum(mid, p+q, 2*(x+y), x+y+q-1) == floor_sum(mid, p+q, 2*(x+y), x)) l = mid;
    else r = mid;
  }

  ll distsum = 2*(r-1)*(x+y) + x;
  if(distsum % (p+q) >= p) printf("%lld\n", distsum);
  else printf("%lld\n", distsum + (p - distsum % (p+q)));
}

int main(){
  int t;
  scanf("%d\n", &t);
  for(int i=0;i<t;i++) solve();
}

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int main(){
	int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

	if(x1==x2 && y1==y2){
		puts("0");
	}
	else if(x1+y1==x2+y2 || x1-y1==x2-y2 || abs(x1-x2)+abs(y1-y2)<=3){
		puts("1");
	}
	else if((x1+y1)%2==(x2+y2)%2){
		puts("2");
	}
	else{
		int ans=3;
		for(int dx=-3;dx<=3;dx++) for(int dy=-3;dy<=3;dy++) if(abs(dx)+abs(dy)<=3) {
			int x3,y3;
			x3=x1+dx; y3=y1+dy;
			if(x3+y3==x2+y2 || x3-y3==x2-y2 || abs(x3-x2)+abs(y3-y2)<=3){
				ans=2;
			}
			x3=x2+dx; y3=y2+dy;
			if(x1+y1==x3+y3 || x1-y1==x3-y3 || abs(x1-x3)+abs(y1-y3)<=3){
				ans=2;
			}
		}
		printf("%d\n",ans);
	}

	return 0;
}
