#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")  
#pragma GCC target("avx,avx2,fma")
 
#define f(i,a,b) for(ll  i = a; i < (ll) b ; i++ ) 
#define af(i,a,b) for(ll i = a; i >= (ll) b ; i--)
#define rep(i,a,b,k) for(ll  i = a; i < (ll) b ; i+= k )
#define arep(i,a,b,k) for(ll i = a; i >= (ll) b ; i-= k)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define sz(a) (ll) a.size()
#define sor(a) sort( a.begin(), a.end() )
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ller   ios::sync_with_stdio(false);cin.tie(0)
 
// policy-based
 
 
 
 
using namespace std;
 
typedef long long ll; // ll or ll
typedef long double ld;
typedef pair<ll,ll> ii ;
typedef vector<ll>  vi ;
typedef vector<ii> vii ;

const ll MAX = 3e5 + 20;
const ll mod = 1e9 + 7;
const ll inf = 2e18;

// C function for extended Euclidean Algorithm
ll gcdExtended(ll a, ll b, ll* x, ll* y){
    // Base Case
    if (a == 0) 
    {
        *x = 0, *y = 1;
        return b;
    }
 
    ll x1, y1; // To store results of recursive call
    ll gcd = gcdExtended(b % a, a, &x1, &y1);
 
    // Update x and y using results of recursive
    // call
    *x = y1 - (b / a) * x1;
    *y = x1;
 
    return gcd;
}
ll tc,x,y,p,q;	

ll modInverse(ll a, ll m){

    ll x, y;
    ll g = gcdExtended(a, m, &x, &y);
    ll res = (x % m + m) % m;
    return res;
}
 
int main(){ 
	fastio;
	cin >> tc;
	while(tc--){
		cin >> x >> y >> p >> q;
		ll ans = inf;
		f(i,0,y){
			f(j,0,q){
				ll m1 = 2*(x+y),m2 = p+q,r1 = x+i,r2 = p + j;
				ll g = __gcd(m1,m2);
				if(r1%g != r2%g) continue;
				ll pp = m1/g, qq = m2/g,rr = (r1-r2)/g;
				ll alfa = ( (rr%pp + pp)%pp * modInverse(qq,pp))%pp;
				ll aux = m2*alfa + r2;
			
				ans = min(ans,m2*alfa + r2);
			}
		}
		if(ans == inf) cout << "infinity\n";
		else
			cout << ans << endl;
	}
	return 0;
}

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
//#define llu unsigned long long
#define int long long
//#define int __int128
//#define double long double
//#define double long long
#define re register int
//#define void inline void
#define eps 1e-8
//#define mod 1e9+7
#define ls(p) p<<1
#define rs(p) p<<1|1
#define pi acos(-1.0)
#define pb push_back
#define mk make_pair
#define P pair < int , int >
using namespace std;
const int mod=1e9+7;
const int M=1e8;
const int N=5e5+5;//??????.???? 4e8
int n;
struct node
{
	int ver,edge,next;
}e[N];
int tot,head[N];
int d[N],f[N];
int ret=1,ans;
void add(int x,int y,int z)
{
	e[++tot].ver=y;
	e[tot].edge=z;
	e[tot].next=head[x];
	head[x]=tot;
}
void addedge(int x,int y,int z)
{
	add(x,y,z);add(y,x,z);
}
void dfs(int x,int pre)
{
	for(re i=head[x];i;i=e[i].next)
	{
		int y=e[i].ver;
		int z=e[i].edge;
		if(y==pre)  continue;
		d[y]=d[x]^z;
		dfs(y,x);
	}
}
void solve()
{
	cin>>n;
	for(re i=1;i<n;i++)
	{
		int x,y,z;
		scanf("%lld%lld%lld",&x,&y,&z);
		addedge(x,y,z);
	}
	dfs(1,1);
	for(re i=0;i<=60;i++)
	{
		int cnt=0;
		for(re j=1;j<=n;j++)  if((d[j]>>i)&1)  cnt++;
		ans=(ans+(ret*cnt)%mod*(n-cnt))%mod;
		ret=(ret*2)%mod;
	}
	cout<<ans<<endl;
}
signed main()
{
    int T=1;
//    cin>>T;
    for(int index=1;index<=T;index++)
    {
        solve();
//        puts("");
    }
    return 0;
}
/*

5 10
-1 -5 -3 5 -1
1



*/