#include<iostream>
#include<cstring>
using namespace std;
class node{
public:
char v;
node *l;
node *r;
};
string s;
int i;
node* build(node* r){
if(s[i]=='#'){
i++;
r=NULL;
}
else {
r=new node;
r->v=s[i++];
r->l=build(r->l);
r->r=build(r->r);
}
return r;
}
void print(node* root){
cout<<root->v;
if(root->l!=NULL) print(root->l);
if(root->r!=NULL) print(root->r);
}
int lmax,rmax;
char lstr,rstr;
void lMax(node* root,int now){
if(now>lmax) lmax=now,lstr=root->v;
if(root->l!=NULL) lMax(root->l,now+1);
if(root->r!=NULL) lMax(root->r,now+1);
}
void rMax(node* root,int now){
if(now>rmax) rmax=now,rstr=root->v;
if(root->l!=NULL) rMax(root->l,now+1);
if(root->r!=NULL) rMax(root->r,now+1);
}
int Max(node* root){
lmax=0;rmax=0;
if(root->l==NULL) lmax=0,lstr=root->v;
else lMax(root->l,1);
if(root->r==NULL) rmax=0,rstr=root->v;
else rMax(root->r,1);
if(lmax==rmax&&lmax==0) return 0;
else return lmax+rmax;
}
int AMax;
char Lstr,Rstr;
void dfs(node* root){
int l=Max(root);
if(l>AMax) {
AMax=l;
Lstr=lstr;
Rstr=rstr;
}
if(root->l!=NULL) dfs(root->l);
if(root->r!=NULL) dfs(root->r);
}
int main(){
int t;
cin>>t;
while(t--){
i=0;
cin>>s;
node *root=new node;
root=build(root);
AMax=0;
dfs(root);
if(AMax==0) cout<<"0:"<<endl;
else cout<<AMax<<":"<<Lstr<<" "<<Rstr<<endl;
}
return 0;
}