//这道题做了很久啊,后来发现是判断除数是否为0时逻辑出问题了
#include <bits/stdc++.h>
using namespace std;
#define int long long

void minus_(int x, int y) {
    bool m = (x > 0) ^ (y > 0);
    if (m) cout << "-";
}//判断符号

signed main() {
    int T;
    cin >> T;
    while (T-- > 0) {
        int a, b, op, c, d;
        cin >> a >> b >> op >> c >> d;
        if (b == 0 || d == 0) {
            cout << "inf"<<endl;continue;
        }
        switch (op) {
            case 1: {
                int x = a * d + c * b;
                if (x) {
                    minus_(b * d, x);
                    cout << llabs(x / gcd(x, b * d)) << " " << llabs(b * d / gcd(x, b * d));
                } else cout << 0 << " " << 1;
                break;
            }
            case 2: {
                int y = a * d - c * b;
                if (y) {
                    minus_(b * d, y);
                    cout << llabs(y / gcd(y, b * d)) << " " << llabs(b * d / gcd(y, b * d));
                } else cout << 0 << " " << 1;
                break;
            }
            case 3: {
                int p = a * c;
                if (p) {
                    minus_(b * d, p);
                    cout << llabs(p / gcd(p, b * d)) << " " << llabs(b * d / gcd(p, b * d));
                } else cout << 0 << " " << 1;
                break;
            }
            case 4: {
                if (c == 0){
                    cout << "inf";break;
                }//一定要先判断c再判断q啊,不然就会和苯人一样爽爽坐牢
                int q = a * d;
                if (q) {
                    minus_(b * c, q);
                    cout << llabs(q / gcd(q, b * c)) << " " << llabs(b * c / gcd(q, b * c));
                } else cout << 0 << " " << 1;
                break;
            }
        }
        cout << endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")