#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
using namespace std;
double W[9];
int x[4][4]={{0,1,1,1},{1,0,2,2},{1,2,0,3},{1,2,3,0}};
int y[4][4]={{0,11,16,21},{11,0,16,21},{16,16,0,21},{21,21,21,0}};
int mx[6]={1,1,1,2,2,3},my[6]={11,16,21,16,21,21};
int ax[12]={1,1,1,1,1,1,2,2,2,2,3,3},ay[12]={11,13,16,18,21,23,16,18,21,23,21,23};
string s[5];
int winner;
double dfs(int id)
{
	if(id==12)
	{
		vector<pair<pair<int,pair<int,int> >,int> >A(4);
		for(int i=0;i<4;i++)
		{
			int p=0,d=0,g=0;
			for(int j=0;j<4;j++)
			{
				if(i==j)continue;
				int a=s[x[i][j]][y[i][j]]-'0';
				int b=s[x[i][j]][y[i][j]+2]-'0';
				if(x[i][j]>i)
				{
					p+=a>b?3:a==b?1:0;
					d+=a-b;
					g+=a;
				}
				else
				{
					p+=b>a?3:b==a?1:0;
					d+=b-a;
					g+=b;
				}
			}
			A[i]=make_pair(make_pair(-p,make_pair(-d,-g)),i);
		}
		sort(A.begin(),A.end());
		vector<int>B;
		int p=0;
		for(int i=1;i<=4;i++)
		{
			if(i<4&&A[p].first==A[i].first)continue;
			else
			{
				if(p+1==i)B.push_back(A[p].second);
				else
				{
					vector<int>I;
					for(int j=p;j<i;j++)I.push_back(A[j].second);
					int pre=114514;
					while(I.size()<pre)
					{
						pre=I.size();
						vector<pair<pair<int,pair<int,int> >,int> >C;
						for(int j=0;j<I.size();j++)
						{
							int p=0,d=0,g=0;
							for(int k=0;k<I.size();k++)
							{
								if(j==k)continue;
								int a=s[x[I[j]][I[k]]][y[I[j]][I[k]]]-'0';
								int b=s[x[I[j]][I[k]]][y[I[j]][I[k]]+2]-'0';
								if(x[I[j]][I[k]]>I[j])
								{
									p+=a>b?3:a==b?1:0;
									d+=a-b;
									g+=a;
								}
								else
								{
									p+=b>a?3:b==a?1:0;
									d+=b-a;
									g+=b;
								}
							}
							C.push_back(make_pair(make_pair(-p,make_pair(-d,-g)),I[j]));
						}
						sort(C.begin(),C.end());
						int q=0;
						vector<int>J;
						for(int j=1;j<=C.size();j++)
						{
							if(j<C.size()&&C[q].first==C[j].first)continue;
							else
							{
								if(q+1==j)B.push_back(C[q++].second);
								else
								{
									bool flag=0;
									for(int k=0;k<I.size();k++)flag|=I[k]==winner;
									if(!flag)
									{
										for(;q<j;q++)B.push_back(C[q].second);
									}
									else
									{
										for(;q<j;q++)J.push_back(C[q].second);
										break;
									}
								}
							}
						}
						I=J;
					}
					if(!I.empty())
					{
						bool flag=0;
						for(int j=0;j<I.size();j++)flag|=I[j]==winner;
						if(!flag)
						{
							for(int j=0;j<I.size();j++)B.push_back(I[j]);
						}
						else
						{
							if(B.size()>=2)return 0;
							else
							{
								double rest=2-B.size();
								return rest/I.size();
							}
						}
					}
				}
				p=i;
			}
		}
		return B[0]==winner||B[1]==winner;
	}
	else
	{
		if(s[ax[id]][ay[id]]!='_')return dfs(id+1);
		else
		{
			double ret=0;
			for(char c='0';c<='8';c++)
			{
				s[ax[id]][ay[id]]=c;
				ret+=W[c-'0']*dfs(id+1);
				s[ax[id]][ay[id]]='_';
			}
			return ret;
		}
	}
}
main()
{
	for(int p=0;p<=8;p++)
	{
		W[p]=1;
		for(int j=1;j<=8;j++)W[p]*=j;
		for(int j=1;j<=p;j++)W[p]/=j;
		for(int j=1;j<=8-p;j++)W[p]/=j;
		for(int j=1;j<=p;j++)W[p]/=4;
		for(int j=1;j<=8-p;j++)W[p]*=3./4;
	}
	int T;cin>>T;
	for(;T--;)
	{
		for(int i=0;i<5;i++)
		{
			cin>>s[i];
			if(s[i][0]=='*')winner=i-1;
		}
		cout<<fixed<<setprecision(9)<<dfs(0)<<endl;
	}
}



Measured memory_bytes: 3192