#include<bits/stdc++.h>
using namespace std;
long long w,k;
string x,y;
long long jinzhi(string x,int y) {
int p=0,ans=0;
for(int i=x.size()-1; i>=0; i--) {
int m=x[i]-'0';
ans+=pow(y,p)*m;
p++;
}
return ans;
}
int main() {
cin>>k;
while(k--) {
bool m=false;
cin>>x>>y;
if(x>y) swap(x,y),m=true;
for(int i=10; i<=15000; i++) {
w=jinzhi(x,i);
int l=10,r=15000,mid;
while(l<r) {
mid=(l+r)/2;
if(jinzhi(y,mid)<w) l=mid+1;
else r=mid;
}
if(jinzhi(y,mid)==w) {
if(m) cout<<mid<<" "<<i<<endl;
else cout<<i<<" "<<mid<<endl;
break;
}
}
}
return 0;
}