/**
* author: shafi
* created: 06.03.2024 16.49.25
**/
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
ll gcd(ll a, ll b){
ll result = min(a, b);
while (result > 0) {
if (a % result == 0 && b % result == 0) {
break;
}
result--;
}
return result;
}
int main(){
ios :: sync_with_stdio(false);
cin.tie(0);
int tc;
cin>>tc;
while(tc--){
ll a,b,c;
cin>>a>>b>>c;
ll x;
cin>>x;
ll ans = (a*b)/gcd(a,b);
ll ans2 = (ans*c)/gcd(ans,c);
if(ans2 < x) cout<<ans2<<endl;
else cout<<"-1\n";
}
return 0;
}