#include <iostream>
#include <cmath>
using namespace std;
long long gcd(long long a, long long b) {
a = abs(a);
b = abs(b);
while (b != 0) {
long long t = b;
b = a % b;
a = t;
}
return a;
}
void G(long long& a, long long& b) {
if (b == 0) {
if (a == 0) {
cout << "nan" << endl;
} else {
cout << "inf" << endl;
}
return;
}
long long g = gcd(a, b);
if (g == 0) {
cout << "0 1" << endl;
return;
}
a /= g;
b /= g;
if (b < 0) {
a = -a;
b = -b;
}
cout << a << " " << b << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
long long a, b, c, d, op;
cin >> a >> b >> op >> c >> d;
long long m, n;
if (op == 1) {
m = a * d + b * c;
n = b * d;
} else if (op == 2) {
m = a * d - b * c;
n = b * d;
} else if (op == 3) {
m = a * c;
n = b * d;
} else {
if (d == 0 || c == 0) {
cout << "inf" << endl;
continue;
}
m = a * d;
n = b * c;
}
if (n == 0) {
cout << "inf" << endl;
} else {
G(m, n);
}
}
return 0;
}
// 64 位输出请用 printf("%lld")