#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int T,I,Br;
    cin >> T;
    
    unsigned long long A[T];
    unsigned long long B[T];
    unsigned long long X[T];
    unsigned long long Y[T];
    
    for ( I = 0; I < T; I++ )
    {
        cin>>A[I]>>B[I];
    }
    
    for ( int P = 0; P < T; P++ )
    {
        for ( Y[P] = 1; Y[P] <= (floor(sqrt(A[P]))); Y[P]++ )
        {
            
            if(A[P]%Y[P] == 0)
            {
               X[P] = A[P]/Y[P];
               
               if( (X[P] ^ Y[P]) == B[P])
                {
                std::cout << Y[P]<< " " <<X[P] << std::endl;
                break;
                }
            
            }
            
            
        }
    }
        
    
    return 0;
}