#include<iostream>
#include<algorithm>
using namespace std;

int main(){
  int t;
  int weight[10];
  int point[4][13];
  int s[5],n[5];
  int kind[4][13];
  string card;
  bool flag;
  bool f = false;

  while(cin >> t){
    if(f)cout << endl;
    f = true;

    for(int i=0;i<4;i++){
      for(int j=0;j<13;j++)cin >> point[i][j];
    }
    weight[0] = 0;
    for(int i=1;i<10;i++)cin >> weight[i];
    
    while(t--){
      for(int i=0;i<4;i++){
	for(int j=0;j<13;j++)kind[i][j] = false;
      }

      for(int i=0;i<5;i++){
	cin >> card;
	if(card[0] == 'A')n[i] = 1;
	else if(card[0] == 'T')n[i] = 10;
	else if(card[0] == 'J')n[i] = 11;
	else if(card[0] == 'Q')n[i] = 12;
	else if(card[0] == 'K')n[i] = 13;
	else n[i] = card[0] - '0';

	if(card[1] == 'S')s[i] = 0;
	else if(card[1] == 'C')s[i] = 1;
	else if(card[1] == 'H')s[i] = 2;
	else if(card[1] == 'D')s[i] = 3;

	kind[s[i]][n[i]-1] = true;
      }

      int ans = 0;
      for(int i=0;i<4;i++){
	for(int j=0;j<13;j++){
	  if(kind[i][j])ans += point[i][j];
	}
      }

      flag = false;
      int c = 9;
      //loyal straight flash
      for(int i=0;i<4;i++){
	if(kind[i][0] && kind[i][9] && kind[i][10] && kind[i][11] && kind[i][12]){
	  flag = true;
	}
      }
      if(!flag){
	c--;
	//straight flash
	for(int i=0;i<4;i++){
	  for(int j=0;j<9;j++){
	    if(kind[i][j] && kind[i][j+1] && kind[i][j+2] && kind[i][j+3] && kind[i][j+4])flag = true;
	  }
	}
	if(!flag){
	  c--;
	  //four card
	  sort(n,n+5);
	  if(n[0] == n[1] && n[1] == n[2] && n[2] == n[3])flag = true;
	  if(n[1] == n[2] && n[2] == n[3] && n[3] == n[4])flag = true;
	  if(!flag){
	    c--;
	    //full house
	    if(n[0] == n[1] && n[1] == n[2] && n[3] == n[4])flag = true;
	    if(n[0] == n[1] && n[2] == n[3] && n[3] == n[4])flag = true;
	    if(!flag){
	      c--;
	      //flash
	      if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3] && s[3] == s[4])flag = true;
	      if(!flag){
		c--;
		//straight
		if(n[0] == n[1]-1 && n[1] == n[2]-1 && n[2] == n[3]-1 && n[3] == n[4]-1)flag = true;
		if(n[0] == 1 && n[1] == 10 && n[2] == 11 && n[3] == 12 && n[4] == 13)flag = true;
		if(!flag){
		  c--;
		  //three card
		  if(n[0] == n[1] && n[1] == n[2])flag = true;
		  if(n[1] == n[2] && n[2] == n[3])flag = true;
		  if(n[2] == n[3] && n[3] == n[4])flag = true;
		  if(!flag){
		    c--;
		    //two pair
		    if(n[0] == n[1] && n[2] == n[3])flag = true;
		    if(n[0] == n[1] && n[3] == n[4])flag = true;
		    if(n[1] == n[2] && n[3] == n[4])flag = true;
		    if(!flag){
		      c--;
		      if(n[0] == n[1])flag = true;
		      if(n[1] == n[2])flag = true;
		      if(n[2] == n[3])flag = true;
		      if(n[3] == n[4])flag = true;
		      if(!flag)c--;
		    }
		  }
		}
	      }
	    }
	  }
	}
      }
      cout << ans * weight[c] << endl;
    }

  }
}