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

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

    vector<long long> resenjeX;
    vector<long long> resenjeY;

    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));

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

        for (x; x >= downX; x -= 2)
            if (((a / x) ^ x) == b) {
                resenjeX.push_back(x);
                resenjeY.push_back(a / x);
                break;
            }
    }

    for (int i = 0; i < resenjeX.size(); i++)
        cout << resenjeX[i] << " " << resenjeY[i] << "\n";
}