#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;

    for (int i = 0; i < t; i++) {
        long long a, b;
        cin >> a >> b;

        long long upX = (long long) sqrt(a);
        long long downX = (long long) pow(2, (long long) log2(upX + 1));

        // cout << upX << " " << downX << "\n";

        long long x = upX;
        if (!(upX % 2)) x--;

        for (x; x >= downX; x -= 2) {
            if (((a / x) ^ x) == b) {
                cout << x << " " << (a / x) << "\n";
                break;
            }
            // cout << x << "\n";
        }
    }
}