#include<string.h>
char*D="A23456789TJQK",*S="SCHD",c[15];
b[4][13],g[10],h[5],F;
main(N){
	int i,j;
	for(;~scanf("%d",&N);){
		if(F)
			puts("");
		F=1;
		for(i=0;i<4;i++)
			for(j=0;j<13;j++)
				scanf("%d",&b[i][j]);
		for(i=1;i<=9;i++)
			scanf("%d",g+i);
		for(;N--;){
			char H[3],*t;
			int d,s0,s,B=0,f=1,r;
			memset(c,'0',13);
			memset(h,0,sizeof(h));
			for(i=0;i<5;i++){
				scanf("%s",H);
				d=strchr(D,H[0])-D;
				s=strchr(S,H[1])-S;
				B+=b[s][d];
				c[d]++;
				if(i==0)
					s0=s;
				else if(s!=s0)
					f=0;
			}
			c[13]=c[0];
			for(i=0;i<13;i++)
				h[c[i]-'0']++;
			t=strstr(c,"11111");
			if(f&&t==c+9)
				r=9;//royal-straight-flush
			else if(f&&t)
				r=8;//straight-flush
			else if(h[4])
				r=7;//four-card
			else if(h[3]&&h[2])
				r=6;//full-house
			else if(f)
				r=5;//flush
			else if(t)
				r=4;//stright
			else if(h[3])
				r=3;//three-card
			else if(h[2]==2)
				r=2;//two-pair
			else if(h[2])
				r=1;//one-pair
			else
				r=0;//no-role
			printf("%d \n",g[r]*B);
		}
	}
}