D="A23456789TJQK",S="SCHD";
b[5][13],g[10],F;
d,s0,s,B,f,r;
i,j;
main(N){
	for(;~scanf("%d",&N);){
		if(F)
			puts("");
		F=1;
		for(i=0;i<61;i++)
			scanf("%d",b[i/13]+i%13);
		for(;N--;){
			char*t,H[3],c[]="00000000000000",h[5]={0};
			B=0;
			f=1;
			memset(c,48,13);
			for(i=5;i--;i>3?s0=s:s-s0?f=0:0)
				scanf("%s",H),c[d=strchr(D,*H)-D]++,B+=b[s=strchr(S,H[1])-S][d];
			for(c[i=13]=*c;i--;t=strstr(c,"11111"))
				h[c[i]-48]++;
			if(f){
				if(t==c+9)
					r=9;
				else if(t)
					r=8;
				else
					r=5;
			}else if(t)
				r=4;
			else if(h[4])
				r=7;
			else if(h[3]){
				if(h[2])
					r=6;
				else
					r=3;
			}else if(h[2])
				r=h[2];
			else
				r=0;
			printf("%d \n",b[4][r?r-1:9]*B);
		}
	}
}