#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define int LL
LL power(int base , int expo)
{
LL ret = 1, to = base;
while(expo)
{
if(expo&1)
ret = ret * to;
to = to * to;
expo/=2;
}
return ret;
}
LL dp[66][2];
pair < int , int > pi[66][2];
LL x[66][2];
LL y[66][2];
LL ret( string A , string B)
{
LL i , j ,k,l;
const LL INF = 20000000000000LL;
for(i=0;i<64;++i)
for(j=0;j<2;++j)
x[i][j]= y[i][j]=dp[i][j] = INF;
dp[A.length()][1] = 0 ;
dp[A.length()][0] = 0 ;
for(i=A.length()-1;i>=0;--i)
for(j=0;j<2;++j)
{
for(k=0;k<10;++k)
for(l=0;l<=(j?9:k);++l)
{
if(A[i]!='?' && A[i]-'0'!=k)continue;
if(B[i]!='?' && B[i]-'0'!=l)continue;
if(dp[i][j] > dp[i+1][j ||(k>l)] + (k-l)*power(10,int(A.length())-1-i ))
{
dp[i][j] = dp[i+1][j ||(k>l)] + (k-l)*power(10,int(A.length())-1-i );
pi[i][j] = {i+1,j ||(k>l)};
x[i][j] = k;
y[i][j] = l ;
}
else if(dp[i][j] == dp[i+1][j ||(k>l)] + (k-l)*power(10,int(A.length())-1-i ))
{
string one = "";
one+=char(x[i][j]+'0');
one += char(y[i][j]+'0');
string two ="";
two+= char(k+'0');
two += char(l+'0');
if(two<one)
{
x[i][j] = k;
y[i][j] = l;
}
}
}
}
return dp[0][0];
}
LL ret2( string A , string B)
{
LL i , j ,k , l ;
const LL INF = 20000000000000LL;
for(i=0;i<64;++i)
for(j=0;j<2;++j)
x[i][j]= y[i][j]=dp[i][j] = INF;
dp[A.length()][1] = 0 ;
dp[A.length()][0] = 0 ;
for(i=A.length()-1;i>=0;--i)
for(j=0;j<2;++j)
{
for(k=0;k<10;++k)
for(l=0;l<=(j?9:k);++l)
{
if(A[i]!='?' && A[i]-'0'!=k)continue;
if(B[i]!='?' && B[i]-'0'!=l)continue;
if(dp[i][j] > dp[i+1][j ||(k>l)] + (k-l)*power(10,int(A.length())-1-i ))
{
dp[i][j] = dp[i+1][j ||(k>l)] + (k-l)*power(10,int(A.length())-1-i );
pi[i][j] = {i+1,j ||(k>l)};
x[i][j] = k;
y[i][j] = l ;
}
else if(dp[i][j] == dp[i+1][j ||(k>l)] + (k-l)*power(10,int(A.length())-1-i ))
{
string one = "";
one+=char(x[i][j]+'0');
one += char(y[i][j]+'0');
string two ="";
two+= char(k+'0');
two += char(l+'0');
reverse(one.begin(),one.end());
reverse(two.begin(),two.end());
if(two<one)
{
x[i][j] = k;
y[i][j] = l;
}
}
}
}
return dp[0][0];
}
main()
{
// freopen("B-small-attempt5.in","r+",stdin);
// freopen("out3.txt","w+",stdout);
int T , i , e , j ;
cin>>T;
for(e=0;e<T;++e)
{
int i ;
cout<<"Case #"<<e+1<<": ";
string A , B;
cin>>A>>B;
LL foo = ret(A,B);
LL bar= ret2(B,A);
assert(foo!=20000000000000LL || bar!=20000000000000LL);
if(foo<=bar)
{
ret(A,B);
i = 0 , j = 0;
string o1="" , o2 = "";
for(;i!=A.length();++i)
{
o1 = o1 + char(x[i][j]+'0');
o2 = o2 + char(y[i][j]+'0');
j = pi[i][j].second;
}
reverse(o1.begin(),o1.end());
reverse(o2.begin(),o2.end());
while(o1.size()<A.size())o1+='0';
while(o2.size()<A.size())o1+='0';
reverse(o1.begin(),o1.end());
reverse(o2.begin(),o2.end());
cout<<o1<<" "<<o2<<"\n";
}
else
{
ret2(B,A);
i = 0 , j = 0;
string o1="" , o2 = "";
for(;i!=A.length();++i)
{
o1 = o1 + char(x[i][j]+'0');
o2 = o2 + char(y[i][j]+'0');
j = pi[i][j].second;
}
reverse(o1.begin(),o1.end());
reverse(o2.begin(),o2.end());
while(o1.size()<A.size())o1+='0';
while(o2.size()<A.size())o1+='0';
reverse(o1.begin(),o1.end());
reverse(o2.begin(),o2.end());
cout<<o2<<" "<<o1<<"\n";
}
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdHlwZWRlZiBsb25nIGxvbmcgTEw7CiNkZWZpbmUgaW50IExMCkxMIHBvd2VyKGludCBiYXNlICwgaW50IGV4cG8pCnsKICAgIExMIHJldCA9IDEsIHRvID0gYmFzZTsKICAgIHdoaWxlKGV4cG8pCiAgICB7CiAgICAgICAgaWYoZXhwbyYxKQogICAgICAgICAgICByZXQgPSByZXQgKiB0bzsKICAgICAgICB0byA9IHRvICogdG87CiAgICAgICAgZXhwby89MjsKICAgIH0KICAgIHJldHVybiByZXQ7Cn0KCgoKTEwgZHBbNjZdWzJdOwpwYWlyICA8IGludCAsIGludCA+IHBpWzY2XVsyXTsKTEwgeFs2Nl1bMl07CkxMIHlbNjZdWzJdOwoKTEwgcmV0KCBzdHJpbmcgQSAsIHN0cmluZyBCKQp7CiAgICBMTCBpICwgaiAsayxsOwogICAgY29uc3QgTEwgSU5GID0gMjAwMDAwMDAwMDAwMDBMTDsKICAgIGZvcihpPTA7aTw2NDsrK2kpCiAgICAgICAgZm9yKGo9MDtqPDI7KytqKQogICAgICAgICAgICB4W2ldW2pdPSB5W2ldW2pdPWRwW2ldW2pdID0gIElORjsKICAgIGRwW0EubGVuZ3RoKCldWzFdID0gMCA7CiAgICBkcFtBLmxlbmd0aCgpXVswXSA9IDAgOwogICAgZm9yKGk9QS5sZW5ndGgoKS0xO2k+PTA7LS1pKQogICAgICAgIGZvcihqPTA7ajwyOysraikKICAgICAgICB7CiAgICAgICAgICAgIGZvcihrPTA7azwxMDsrK2spCiAgICAgICAgICAgICAgICBmb3IobD0wO2w8PShqPzk6ayk7KytsKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGlmKEFbaV0hPSc/JyAmJiBBW2ldLScwJyE9ayljb250aW51ZTsKICAgICAgICAgICAgICAgICAgICBpZihCW2ldIT0nPycgJiYgQltpXS0nMCchPWwpY29udGludWU7CgogICAgICAgICAgICAgICAgICAgIGlmKGRwW2ldW2pdID4gZHBbaSsxXVtqIHx8KGs+bCldICsgKGstbCkqcG93ZXIoMTAsaW50KEEubGVuZ3RoKCkpLTEtaSAgKSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBkcFtpXVtqXSA9IGRwW2krMV1baiB8fChrPmwpXSArIChrLWwpKnBvd2VyKDEwLGludChBLmxlbmd0aCgpKS0xLWkgICk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBwaVtpXVtqXSA9IHtpKzEsaiB8fChrPmwpfTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHhbaV1bal0gPSBrOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgeVtpXVtqXSA9IGwgOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBlbHNlIGlmKGRwW2ldW2pdID09IGRwW2krMV1baiB8fChrPmwpXSArIChrLWwpKnBvd2VyKDEwLGludChBLmxlbmd0aCgpKS0xLWkgICkpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBzdHJpbmcgb25lID0gIiI7CiAgICAgICAgICAgICAgICAgICAgICAgIG9uZSs9Y2hhcih4W2ldW2pdKycwJyk7CiAgICAgICAgICAgICAgICAgICAgICAgIG9uZSArPSBjaGFyKHlbaV1bal0rJzAnKTsKICAgICAgICAgICAgICAgICAgICAgICAgc3RyaW5nIHR3byA9IiI7CiAgICAgICAgICAgICAgICAgICAgICAgIHR3bys9IGNoYXIoaysnMCcpOwogICAgICAgICAgICAgICAgICAgICAgICB0d28gKz0gY2hhcihsKycwJyk7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKHR3bzxvbmUpCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHhbaV1bal0gPSBrOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgeVtpXVtqXSA9IGw7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgfQogICAgcmV0dXJuIGRwWzBdWzBdOwp9CkxMIHJldDIoIHN0cmluZyBBICwgc3RyaW5nIEIpCnsKICAgIExMIGkgLCBqICxrICwgbCAgOwogICAgY29uc3QgTEwgSU5GID0gMjAwMDAwMDAwMDAwMDBMTDsKICAgIGZvcihpPTA7aTw2NDsrK2kpCiAgICAgICAgZm9yKGo9MDtqPDI7KytqKQogICAgICAgICAgICB4W2ldW2pdPSB5W2ldW2pdPWRwW2ldW2pdID0gIElORjsKICAgIGRwW0EubGVuZ3RoKCldWzFdID0gMCA7CiAgICBkcFtBLmxlbmd0aCgpXVswXSA9IDAgOwogICAgZm9yKGk9QS5sZW5ndGgoKS0xO2k+PTA7LS1pKQogICAgICAgIGZvcihqPTA7ajwyOysraikKICAgICAgICB7CiAgICAgICAgICAgIGZvcihrPTA7azwxMDsrK2spCiAgICAgICAgICAgICAgICBmb3IobD0wO2w8PShqPzk6ayk7KytsKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoQVtpXSE9Jz8nICYmIEFbaV0tJzAnIT1rKWNvbnRpbnVlOwogICAgICAgICAgICAgICAgICAgICAgICBpZihCW2ldIT0nPycgJiYgQltpXS0nMCchPWwpY29udGludWU7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKGRwW2ldW2pdID4gZHBbaSsxXVtqIHx8KGs+bCldICsgKGstbCkqcG93ZXIoMTAsaW50KEEubGVuZ3RoKCkpLTEtaSAgKSkKICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgZHBbaV1bal0gPSBkcFtpKzFdW2ogfHwoaz5sKV0gKyAoay1sKSpwb3dlcigxMCxpbnQoQS5sZW5ndGgoKSktMS1pICApOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgcGlbaV1bal0gPSB7aSsxLGogfHwoaz5sKX07CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB4W2ldW2pdID0gazsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHlbaV1bal0gPSBsIDsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBlbHNlIGlmKGRwW2ldW2pdID09IGRwW2krMV1baiB8fChrPmwpXSArIChrLWwpKnBvd2VyKDEwLGludChBLmxlbmd0aCgpKS0xLWkgICkpCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHN0cmluZyBvbmUgPSAiIjsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG9uZSs9Y2hhcih4W2ldW2pdKycwJyk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvbmUgKz0gY2hhcih5W2ldW2pdKycwJyk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzdHJpbmcgdHdvID0iIjsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHR3bys9IGNoYXIoaysnMCcpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgdHdvICs9IGNoYXIobCsnMCcpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgcmV2ZXJzZShvbmUuYmVnaW4oKSxvbmUuZW5kKCkpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgcmV2ZXJzZSh0d28uYmVnaW4oKSx0d28uZW5kKCkpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYodHdvPG9uZSkKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB4W2ldW2pdID0gazsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB5W2ldW2pdID0gbDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICB9CiAgICByZXR1cm4gZHBbMF1bMF07Cn0KCgogbWFpbigpCnsKLy8gICAgZnJlb3BlbigiQi1zbWFsbC1hdHRlbXB0NS5pbiIsInIrIixzdGRpbik7Ci8vICAgIGZyZW9wZW4oIm91dDMudHh0IiwidysiLHN0ZG91dCk7CiAgICBpbnQgVCAgICwgaSAsIGUgLCBqICA7CiAgICBjaW4+PlQ7CiAgICBmb3IoZT0wO2U8VDsrK2UpCiAgICB7CiAgICAgICAgaW50IGkgOwogICAgICAgIGNvdXQ8PCJDYXNlICMiPDxlKzE8PCI6ICI7CiAgICAgICAgc3RyaW5nIEEgLCBCOwogICAgICAgIGNpbj4+QT4+QjsKCiAgICAgICAgTEwgZm9vID0gcmV0KEEsQik7CiAgICAgICAgTEwgYmFyPSByZXQyKEIsQSk7CgogICAgICAgIGFzc2VydChmb28hPTIwMDAwMDAwMDAwMDAwTEwgfHwgYmFyIT0yMDAwMDAwMDAwMDAwMExMKTsKICAgICAgICBpZihmb288PWJhcikKICAgICAgICB7CiAgICAgICAgICAgIHJldChBLEIpOwoKICAgICAgICAgICAgaSA9IDAgLCBqID0gMDsKICAgICAgICAgICAgc3RyaW5nIG8xPSIiICwgbzIgPSAiIjsKICAgICAgICAgICAgZm9yKDtpIT1BLmxlbmd0aCgpOysraSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgbzEgPSBvMSAgKyBjaGFyKHhbaV1bal0rJzAnKTsKICAgICAgICAgICAgICAgIG8yID0gbzIgICsgY2hhcih5W2ldW2pdKycwJyk7CiAgICAgICAgICAgICAgICBqID0gcGlbaV1bal0uc2Vjb25kOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldmVyc2UobzEuYmVnaW4oKSxvMS5lbmQoKSk7CiAgICAgICAgICAgIHJldmVyc2UobzIuYmVnaW4oKSxvMi5lbmQoKSk7CiAgICAgICAgICAgIHdoaWxlKG8xLnNpemUoKTxBLnNpemUoKSlvMSs9JzAnOwogICAgICAgICAgICB3aGlsZShvMi5zaXplKCk8QS5zaXplKCkpbzErPScwJzsKICAgICAgICAgICAgcmV2ZXJzZShvMS5iZWdpbigpLG8xLmVuZCgpKTsKICAgICAgICAgICAgcmV2ZXJzZShvMi5iZWdpbigpLG8yLmVuZCgpKTsKICAgICAgICAgICAgY291dDw8bzE8PCIgIjw8bzI8PCJcbiI7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CgogICAgICAgICAgICByZXQyKEIsQSk7CiAgICAgICAgICAgIGkgPSAwICwgaiA9IDA7CiAgICAgICAgICAgIHN0cmluZyBvMT0iIiAsIG8yID0gIiI7CiAgICAgICAgICAgIGZvcig7aSE9QS5sZW5ndGgoKTsrK2kpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIG8xID0gbzEgICsgY2hhcih4W2ldW2pdKycwJyk7CiAgICAgICAgICAgICAgICBvMiA9IG8yICArIGNoYXIoeVtpXVtqXSsnMCcpOwogICAgICAgICAgICAgICAgaiA9IHBpW2ldW2pdLnNlY29uZDsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXZlcnNlKG8xLmJlZ2luKCksbzEuZW5kKCkpOwogICAgICAgICAgICByZXZlcnNlKG8yLmJlZ2luKCksbzIuZW5kKCkpOwogICAgICAgICAgICB3aGlsZShvMS5zaXplKCk8QS5zaXplKCkpbzErPScwJzsKICAgICAgICAgICAgd2hpbGUobzIuc2l6ZSgpPEEuc2l6ZSgpKW8xKz0nMCc7CiAgICAgICAgICAgIHJldmVyc2UobzEuYmVnaW4oKSxvMS5lbmQoKSk7CiAgICAgICAgICAgIHJldmVyc2UobzIuYmVnaW4oKSxvMi5lbmQoKSk7CiAgICAgICAgICAgIGNvdXQ8PG8yPDwiICI8PG8xPDwiXG4iOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9Cg==