#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define num first
#define ch  second

enum{S=0,C=1,H=2,D=3};

class card{
public:
  pair<int,char> d[5];

  bool isflush(){
    REP(i,1,5)if (d[0].ch != d[i].ch)return false;
    return true;
  }
  
  bool isspecialst(){
    if (d[0].num==1 && 
	d[1].num==10&&
	d[2].num==11&&
	d[3].num==12&&
	d[4].num==13)return true;
    return false;
  }
  bool isstraight(){
    if (d[0].num+1==d[1].num &&
	d[0].num+2==d[2].num&&
	d[0].num+3==d[3].num&&
	d[0].num+4==d[4].num)return true;
    return isspecialst();
  }
  
  bool islsf(){
    if (isspecialst()&&isflush())return true;
    return false;
  }
  
  bool issf(){
    if (isflush()&&isstraight())return true;
    return false;
  }
  
  //four,three,two,one
  bool isnum(int num){
    rep(i,5){
      int cnt=0;
      rep(j,5)if (d[i].num==d[j].num)cnt++;
      if (cnt == num)return true;
    }
    return false;
  }
  
  bool istwo(){
    if (d[0].num==d[1].num && d[2].num==d[3].num)return true;
    if (d[0].num==d[1].num && d[3].num==d[4].num)return true;
    if (d[1].num==d[2].num && d[3].num==d[4].num)return true;
    return false;
  }
  

  bool isfullhouse(){
    if (d[0].num==d[1].num &&
	d[2].num==d[3].num &&
	d[2].num==d[4].num)return true;
    if (d[0].num==d[1].num &&
	d[0].num==d[2].num &&
	d[3].num==d[4].num)return true;
    return false;
  }
  int judge(int *rate){
    sort(d,d+5);
    if (islsf()){
      return rate[9];
    }
    if (issf())return rate[8];
    if (isnum(4))return rate[7];
    if (isfullhouse())return rate[6];
    if (isflush())return rate[5];
    if (isstraight())return rate[4];
    if (isnum(3))return rate[3];
    if (istwo())return rate[2];
    if (isnum(2))return rate[1];
    return rate[0];
  }

};




int main(){
  string tmp="A23456789TJQK";
  int yaku[255];
  for(int i=0;i<tmp.size();i++){
    yaku[tmp[i]]=i+1;
  }
  
  int tc=0;
  int rate[10];
  int table[4][14];
  int n;
  while(cin>>n){
    if(tc++)cout << endl;

    rep(i,4){
      REP(j,1,14)cin>>table[i][j];
    }

    rate[0]=0;
    for(int i=1;i<10;i++){
      cin>>rate[i];
    }

    card in;
    rep(i,n){
      int score=0;
      rep(j,5){
	char a,b;
	cin>>a>>b;
	in.d[j].num=yaku[a];
	int ac=0;
	if (b=='S')ac=S;
	if (b=='C')ac=C;
	if (b=='H')ac=H;
	if (b=='D')ac=D;
	in.d[j].ch=b;
	//	cout << ac <<" " << yaku[a] << endl;
	score+=table[ac][yaku[a]];
      }
      //      cout <<" score is " << score << " " << in.judge(rate) << endl;
      cout << score*in.judge(rate)<<endl;
    }
  }
  return false;
}