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