#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;

typedef pair<int, char> Card;

map<string, int> point;

Card hand[5];
bool used[5];

void output(){ 
  for(int i = 0 ; i < 5 ; i++){
    cout << hand[i].first << ' ' << hand[i].second << endl;
  }
}


char toChar(int n){
  if(2 <= n && n <= 9) return n+'0';
  else if(n == 1) return 'A';
  else if(n == 10) return 'T';
  else if(n == 11) return 'J';
  else if(n == 12) return 'Q';
  else if(n == 13) return 'K';
}

int toInt(char c){
  if('2' <= c && c <= '9') return c-'0';
  else if(c == 'A') return 1;
  else if(c == 'T') return 10;
  else if(c == 'J') return 11;
  else if(c == 'Q') return 12;
  else if(c == 'K') return 13;
}

bool isOnePair(){
  //output();
  pair<int, int> Max;
  Max.first = -1;
  for(int i = 0 ; i < 5 ; i++){
    if(used[i]) continue;
    int tmp = 0;
    for(int j = 0 ; j < 5 ; j++){
      if(i == j) continue;
      if(hand[i].first == hand[j].first) tmp++;
    }
     
    if(tmp == 1){
      Max.first = tmp;
      Max.second = hand[i].first;
    }
  }
  
  if(Max.first != 1) return false;
  
  for(int i = 0 ; i < 5 ; i++){
    if(Max.second == hand[i].first){
      used[i] = true;
    }
  }
  return true;
}

bool isThreeCard(){
  pair<int, int> Max;
  Max.first = -1;
  for(int i = 0 ; i < 5 ; i++){
    if(used[i]) continue;
    int tmp = 0;
    for(int j = 0 ; j < 5 ; j++){
      if(i == j) continue;
      if(hand[i].first == hand[j].first) tmp++;
    }
    if(tmp == 2){
      Max.first = tmp;
      Max.second = hand[i].first;
    }
  }
  
  if(Max.first != 2) return false;
  
  for(int i = 0 ; i < 5 ; i++){
    if(Max.second == hand[i].first){
      used[i] = true;
    }
  }
  return true;

}

bool isFourCard(){
  pair<int, int> Max;
  Max.first = -1;
  for(int i = 0 ; i < 5 ; i++){
    if(used[i]) continue;
    int tmp = 0;
    for(int j = 0 ; j < 5 ; j++){
      if(i == j) continue;
      if(hand[i].first == hand[j].first) tmp++;
    }
    if(tmp == 3){
      Max.first = tmp;
      Max.second = hand[i].first;
    }
  }
  
  if(Max.first != 3) return false;
  
  for(int i = 0 ; i < 5 ; i++){
    if(Max.second == hand[i].first){
      used[i] = true;
    }
  }
  return true;
}

bool isStraight(){
  if(hand[0].first == 1 && hand[1].first == 10 && hand[2].first == 11 &&
     hand[3].first == 12 && hand[4].first == 13) return true;
  
  for(int i = 0 ; i < 4 ; i++){
    if(hand[i].first+1 != hand[i+1].first) return false;
  }
  
  
  
  return true;
}

bool isFlash(){
  char tmp = hand[0].second;
  for(int i = 0 ; i < 5 ; i++){
    if(hand[i].second != tmp) return false;
  }
  return true;
}

bool isRoyalStraightFlash(){
  if(hand[0].first != 1) return false;
  
  if(hand[0].first == 1){
    for(int i = 1 ; i < 5 ; i++){
      if(hand[i].first != 9+i) return false;
    }
  }
  
  if(hand[0].second == hand[1].second && hand[1].second == hand[2].second &&
     hand[2].second == hand[3].second && hand[3].second == hand[4].second)
    return true;
  
  return false;
}
  

int judgeCard(){
  sort(hand, hand+5);
  memset(used, false, sizeof(used));
  
  bool OnePair1, OnePair2, ThreeCard, FourCard, Straight, Flash;
  OnePair1 = OnePair2 = ThreeCard = FourCard = Straight = Flash = false;
  
  OnePair1 = isOnePair();
  //cout << "OnePair1 " << OnePair2 << endl;
  OnePair2 = isOnePair();
  ThreeCard = isThreeCard();
  //cout << "Three Card " << ThreeCard << endl;
  FourCard = isFourCard();
  Straight = isStraight();
  Flash = isFlash();
  
  if(isRoyalStraightFlash()) return 8;
  else if(Straight && Flash) return 7;
  else if(FourCard) return 6;
  else if(OnePair1 && ThreeCard) return 5;
  else if(Flash) return 4;
  else if(Straight) return 3;
  else if(ThreeCard) return 2;
  else if(OnePair1 && OnePair2) return 1;
  else if(OnePair1) return 0;
  else return 9;
}

int main(){
  int tc;
  bool Flag = false;
  while(cin >> tc){
    if(Flag) cout << endl;
    Flag = true;
    int p;
    for(int color = 0 ; color < 4 ; color++){
      for(int n = 1 ; n <= 13 ; n++){
	cin >> p;
	string card = "";
	card += toChar(n);
	if(color == 0) card += "S";
	else if(color == 1) card += "C";
	else if(color == 2) card += "H";
	else if(color == 3) card += "D";
	point[card] = p;
      }
    }
    
    int power[9];
    for(int i = 0 ; i < 9 ; i++) cin >> power[i];
    
    string tmp[5];
    while(tc--){
      for(int i = 0 ; i < 5 ; i++) cin >> tmp[i];
      for(int i = 0 ; i < 5 ; i++){
	hand[i].first = toInt(tmp[i][0]);
	hand[i].second = tmp[i][1];
      }
      
      int baseSum = 0;
      for(int i = 0 ; i < 5 ; i++){
	baseSum += point[tmp[i]];
      }
      
      int ans;
      
      int what = judgeCard();
      if(what == 9) ans = 0;
      else{
	ans = power[what] * baseSum;
      }
      //cout << "What " << what << endl;
      cout << ans << endl;
      //cout << endl;
    }
    //cout << endl << endl << "#########################"<< endl;
  }
  return 0;
}

Measured memory_bytes: 1224