#include #include #include #include #include using namespace std; const long long M=1LL<<60; // Function to calculate the divisors of a number N vector Divisors(long long N) { vector divisors; long long i; for (i = 1; i * i < N; ++i) { if (N % i == 0) { divisors.push_back(i); } } if (i * i == N) { divisors.push_back(i); } for (--i; i > 0; --i) { if (N % i == 0) { divisors.push_back(N / i); } } return divisors; } // Recursive function to generate tree nodes vector> f(long long p, long long q, const vector& D) { if (q - p == 1) { return { make_tuple(1, p, M) }; } vector> lst; vector mods; for (size_t i = 1; i < D.size(); ++i) { long long d = D[i]; if ((q - p) % d == 0) { mods.push_back((d+(-p) % d)%d); } } long long mi = *min_element(mods.begin(), mods.end()); long long pp = p + mi; long long qq = q + mi; long long g = gcd(pp, qq); lst.push_back(make_tuple(q - p, p, pp)); vector> sub_lst = f(pp / g, qq / g, D); lst.insert(lst.end(), sub_lst.begin(), sub_lst.end()); return lst; } // Function to generate the tree vector> tree(long long p, long long q) { vector D = Divisors(q - p); return f(p, q, D); } int main() { int Q; cin >> Q; for (int q = 0; q < Q; ++q) { long long a, b, c, d; cin >> a >> b >> c >> d; vector> tree0 = tree(a, b); vector> tree1 = tree(c, d); unordered_map>> dct; for (const auto& [d, a, b] : tree0) { dct[d].emplace_back(a, b); } for (const auto& [d, a, b] : tree1) { dct[d].emplace_back(a, b); } vector> lst; for (const auto& [d, v] : dct) { if (v.size() == 2) { auto [a0, b0] = v[0]; auto [a1, b1] = v[1]; long long a = max(a0, a1); long long b = min(b0, b1); if (a <= b) { lst.emplace_back(d, a); } } } auto [y, x] = *min_element(lst.begin(), lst.end()); cout << x << " " << x + y << endl; } return 0; }