#include <iostream>
using namespace std;
long long int gcd(long long int a, long long int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return (a / gcd(a, b)) * b;
}
long long int find_gcm(long long int a, long long int b, long long int c, long long int X) {
long long int gcm_ab = lcm(a, b);
long long int gcm_abc = lcm(gcm_ab, c);
if (gcm_abc < X) {
return gcm_abc;
} else {
return -1;
}
}
int main() {
int T;
cin >> T;
while (T--) {
long long int a, b, c, X;
cin >> a >> b >> c >> X;
long long int result = find_gcm(a, b, c, X);
cout << result << endl;
}
return 0;
}