/* Problem idea by Albert Lai Problem and solution written by Thomas Tang */ #include #include int gcd(int a, int b) { int tmp; a = abs(a); b = abs(b); while (a != 0) { if (a < b) { tmp = a; a = b; b = tmp; } tmp = a/b; a = a - b*tmp; } return b; } main() { int n, op, a1, b1, a2, b2, tmp, x, y, c1, c2; scanf("%d", &n); while (n-- > 0) { scanf("%d%d%d%d%d", &op, &a1, &b1, &a2, &b2); tmp = gcd(a1, b1); a1 /= tmp; b1 /= tmp; tmp = gcd(a2, b2); a2 /= tmp; b2 /= tmp; switch (op) { case 2: a2 = -a2; case 1: tmp = gcd(b1, b2); c1 = b2/tmp; c2 = b1/tmp; y = b1*c1; x = c1*a1 + c2*a2; break; case 3: case 4: if (op == 4) { tmp = b2; b2 = a2; a2 = tmp; } tmp = gcd(a1, b2); a1 /= tmp; b2 /= tmp; tmp = gcd(a2, b1); a2 /= tmp; b1 /= tmp; x = a1*a2; y = b1*b2; break; } tmp = gcd(x, y); x /= tmp; y /= tmp; if (y < 0) { x = -x; y = -y; } printf("%d %d\n", x, y); } }