#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<set>
using namespace std;
// Define Some Variables
#define eps 1e-14
#define si 105
#define pi acos(-1.0)
#define inf (1<<30)-1
#define mod 1000000000 //10^9
//Define Some Functions
#define even(a) ((a)%2==0)
#define odd(a) ((a)%2==1)
#define max(a,b) (a>b ?a:b)
#define min(a,b) (a<b ?a:b)
#define sqr(a)((a)*(a))
#define area(x1,y1,x2,y2,x3,y3) (x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)) //Area of a triangle
#define dist(x1,y1,x2,y2) (sqr(x1-x2)+sqr(y1-y2)) //Distance between two points
#define mem(a,v) memset(a,v,sizeof(a))
inline bool compare( double a, double b ) { return fabs ( a- b ) < 1e-9 ; }
#define fr(i,a,b) for(i=a;i<=b;i++)
#define rep(i,a,b) for(i=a;i<b;i++)
#define rev(i,a,b) for(i=a;i>=b;i--)
typedef long long i64;
int dx[ ] = { 0 ,0 ,1 ,- 1 } ;
int dy[ ] = { 1 ,- 1 ,0 ,0 } ;
int i,j,l,n,cs= 1 ,l1,l2;
char ch1[ si] ,ch2[ si] ,res[ si] ,dpc[ si] [ si] [ si] ;
int dp[ si] [ si] ;
i64 bmod( i64 a,i64 b)
{
if ( b== 0 )
return 1 ;
i64 x= bmod( a,b/ 2 ) ;
x= ( x* x) % mod;
if ( b% 2 == 1 )
x= ( x* a) % mod;
return x;
}
int gcd( int a,int b)
{
while ( b> 0 )
{
a% = b;
a^ = b;
b^ = a;
a^ = b;
}
return a;
}
i64 lcm( i64 a,i64 b)
{
return ( ( a* b) / gcd( a,b) ) ;
}
int rec( int i,int j,char * res)
{
if ( i== l1|| j== l2)
return 0 ;
int & ret= dp[ i] [ j] ;
if ( ret! = - 1 )
{
strcpy ( res,dpc[ i] [ j] ) ;
return ret;
}
int p,q,ll,x;
char st1[ si] ,st2[ si] ;
if ( ch1[ i] == ch2[ j] )
{
ret= 1 + rec( i+ 1 ,j+ 1 ,res) ;
st1[ 0 ] = ch1[ i] ;
ll= strlen ( res) ;
rep( x,0 ,ll)
st1[ x+ 1 ] = res[ x] ;
st1[ ll+ 1 ] = 0 ;
strcpy ( res,st1) ;
strcpy ( dpc[ i] [ j] ,res) ;
}
else
{
strcpy ( st2,res) ;
p= rec( i+ 1 ,j,res) ;
strcpy ( st1,res) ;
// strcpy(res,st2);
q= rec( i,j+ 1 ,st2) ;
// strcpy(st2,res);
if ( p> q)
strcpy ( res,st1) ;
else if ( p< q)
strcpy ( res,st2) ;
else
{
p= strcmp ( st1,st2) ;
if ( p< 0 )
strcpy ( res,st1) ;
else
strcpy ( res,st2) ;
}
ret= max( p,q) ;
strcpy ( dpc[ i] [ j] ,res) ;
}
return ret;
}
int main( )
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int t;
scanf ( "%d" ,& t) ;
while ( t-- )
{
scanf ( "%s%s" ,& ch1,ch2) ;
l1= strlen ( ch1) ;
l2= strlen ( ch2) ;
mem( dp,- 1 ) ;
res[ 0 ] = 0 ;
int ans= rec( 0 ,0 ,res) ;
printf ( "Case %d: " ,cs++ ) ;
if ( ans== 0 )
puts ( ":(" ) ;
else
puts ( res) ;
}
return 0 ;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPG1hdGguaD4KI2luY2x1ZGU8c3RyaW5nLmg+CiNpbmNsdWRlPHN0cmluZz4KI2luY2x1ZGU8c3RkbGliLmg+CiNpbmNsdWRlPG1hcD4KI2luY2x1ZGU8dmVjdG9yPgojaW5jbHVkZTxxdWV1ZT4KI2luY2x1ZGU8c3RhY2s+CiNpbmNsdWRlPGFsZ29yaXRobT4KI2luY2x1ZGU8c2V0Pgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gRGVmaW5lIFNvbWUgVmFyaWFibGVzCiNkZWZpbmUgZXBzIDFlLTE0CiNkZWZ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gICBpZihiPT0wKQogICAgICAgIHJldHVybiAxOwogICAgaTY0IHg9Ym1vZChhLGIvMik7CiAgICB4PSh4KngpJW1vZDsKICAgIGlmKGIlMj09MSkKICAgICAgICB4PSh4KmEpJW1vZDsKICAgIHJldHVybiB4Owp9CgppbnQgZ2NkKGludCBhLGludCBiKQp7CiAgICB3aGlsZShiPjApCiAgICB7CiAgICAgICAgYSU9YjsKICAgICAgICBhXj1iOwogICAgICAgIGJePWE7CiAgICAgICAgYV49YjsKICAgIH0KICAgIHJldHVybiBhOwp9CgoKaTY0IGxjbShpNjQgYSxpNjQgYikKewoJcmV0dXJuICgoYSpiKS9nY2QoYSxiKSk7Cn0KCmludCByZWMoaW50IGksaW50IGosY2hhciogcmVzKQp7CiAgICBpZihpPT1sMXx8aj09bDIpCiAgICByZXR1cm4gMDsKICAgIGludCAmcmV0PWRwW2ldW2pdOwogICAgaWYocmV0IT0tMSkKICAgIHsKICAgICAgICBzdHJjcHkocmVzLGRwY1tpXVtqXSk7CiAgICAgICAgcmV0dXJuIHJldDsKICAgIH0KCiAgICBpbnQgcCxxLGxsLHg7CiAgICBjaGFyIHN0MVtzaV0sc3QyW3NpXTsKICAgIGlmKGNoMVtpXT09Y2gyW2pdKQogICAgewogICAgICAgIHJldD0xK3JlYyhpKzEsaisxLHJlcyk7CiAgICAgICAgc3QxWzBdPWNoMVtpXTsKICAgICAgICBsbD1zdHJsZW4ocmVzKTsKICAgICAgICByZXAoeCwwLGxsKQogICAgICAgIHN0MVt4KzFdPXJlc1t4XTsKICAgICAgICBzdDFbbGwrMV09MDsKICAgICAgICBzdHJjcHkocmVzLHN0MSk7CiAgICAgICAgc3RyY3B5KGRwY1tpXVtqXSxyZXMpOwogICAgfQogICAgZWxzZQogICAgewogICAgICAgIHN0cmNweShzdDIscmVzKTsKCiAgICAgICAgcD1yZWMoaSsxLGoscmVzKTsKICAgICAgICBzdHJjcHkoc3QxLHJlcyk7CgogICAgIC8vICAgc3RyY3B5KHJlcyxzdDIpOwogICAgICAgIHE9cmVjKGksaisxLHN0Mik7CiAgICAgLy8gICBzdHJjcHkoc3QyLHJlcyk7CgogICAgICAgIGlmKHA+cSkKICAgICAgICBzdHJjcHkocmVzLHN0MSk7CiAgICAgICAgZWxzZSBpZihwPHEpCiAgICAgICAgc3RyY3B5KHJlcyxzdDIpOwogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIHA9c3RyY21wKHN0MSxzdDIpOwogICAgICAgICAgICBpZihwPDApCiAgICAgICAgICAgIHN0cmNweShyZXMsc3QxKTsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICBzdHJjcHkocmVzLHN0Mik7CiAgICAgICAgfQoKICAgICAgICByZXQ9bWF4KHAscSk7CiAgICAgICAgc3RyY3B5KGRwY1tpXVtqXSxyZXMpOwogICAgfQogICAgcmV0dXJuIHJldDsKfQoKaW50IG1haW4oKQp7Ci8vCWZyZW9wZW4oImlucHV0LnR4dCIsICJyIiwgc3RkaW4pOwovLwlmcmVvcGVuKCJvdXRwdXQudHh0IiwgInciLCBzdGRvdXQpOwoKICAgIGludCB0OwogICAgc2NhbmYoIiVkIiwmdCk7CiAgIAl3aGlsZSh0LS0pCiAgIAl7CiAgIAkgICAgc2NhbmYoIiVzJXMiLCZjaDEsY2gyKTsKICAgCSAgICBsMT1zdHJsZW4oY2gxKTsKICAgCSAgICBsMj1zdHJsZW4oY2gyKTsKCQltZW0oZHAsLTEpOwoJCXJlc1swXT0wOwoJCWludCBhbnM9cmVjKDAsMCxyZXMpOwoJCXByaW50ZigiQ2FzZSAlZDogIixjcysrKTsKCQlpZihhbnM9PTApCgkJcHV0cygiOigiKTsKCQllbHNlCiAgICAgICAgcHV0cyhyZXMpOwogICAgfQogICAJcmV0dXJuIDA7Cn0K