#include using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); int Q, m = 32000; cin >> Q; vector prime = {2, 3}; vector tb(m); for(int i = 2; i < m; i++){ if(tb[i])continue; prime.push_back(i); for(int j = i; j < m; j += i) tb[j] = true; } while(Q--){ int p0, q0, p1, q1; cin >> p0 >> q0 >> p1 >> q1; while(p0 != p1 || q0 != q1){ int d0 = q0 - p0, d1 = q1 - p1; if(d0 == 1 && d1 == 1){ p0 = max(p0, p1); q0 = p0 + 1; break; } if(d0 < d1 || (d0 == d1 && p0 > p1)){ swap(p0, p1), swap(q0, q1), swap(d0, d1);} int v = d0, add = d0; for(int i = 0; prime[i] * prime[i] <= v; i++){ if(v % prime[i] == 0){ add = min(add, prime[i] - p0 % prime[i]); do{ v /= prime[i]; } while (v % prime[i] == 0); } } if(v != 1) add = min(add, v - p0 % v); p0 += add, q0 += add; if(d0 == d1 && p0 >= p1){ p0 = p1, q0 = q1; break; } int g = __gcd(p0, q0); p0 /= g, q0 /= g; } cout << p0 << " " << q0 << '\n'; } }