#include <bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	bool ctrl;
	unsigned long long a,b;
	cin>>t;
	for(int h=1;h<=t;h++)
	{
		cin>>a>>b;
		ctrl=false;
		for(unsigned long long i=1;i<=sqrt(a);i++)
		{
			unsigned long long x=a/i;
			unsigned long long d=(x^i);
			if(a%i==0 && d==b && ctrl==false)
			{
				ctrl=true;
				cout<<i<<" "<<a/i<<endl;
			}
		}
	}
	return 0;
}