#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>

using namespace std;

int n;
int score[200][200];
int markToNum[200];
int per[10];


bool isOnePair(vector<string> &cards){
    int tmp[100];
    fill(tmp,tmp+100,0);
    for(int i = 0; i < cards.size(); i++)
        tmp[markToNum[cards[i][0]]]++;
    for(int i = 1; i <= 13; i++){
        if(tmp[i]==2)
            return true;
    }
    return false;
}
bool isTwoPair(vector<string> &cards){
    int tmp[100];
    fill(tmp,tmp+100,0);
    for(int i = 0; i < cards.size(); i++)
        tmp[markToNum[cards[i][0]]]++;
    int cnt=0;
    for(int i = 1; i <= 13; i++){
        if(tmp[i]>=2)
            cnt++;
    }
    if(cnt>=2)
        return true;
    return false;
}
bool isThreeCard(vector<string> &cards){
    int tmp[100];
    fill(tmp,tmp+100,0);
    for(int i = 0; i < cards.size(); i++)
        tmp[markToNum[cards[i][0]]]++;
    int cnt=0;
    for(int i = 1; i <= 13; i++){
        if(tmp[i]==3)
            return true;
    }
    return false;
}
bool isStraight(vector<string> &cards){
    vector<int> num;
    for(int i = 0; i < cards.size(); i++)
        num.push_back(markToNum[cards[i][0]]);
    sort(num.begin(),num.end());
    if(num[0]==1){
        if((num[1]==2&&num[2]==3&&num[3]==4&&num[4]==5)||(num[1]==10&&num[2]==11&&num[3]==12&&num[4]==13))
            return true;
        return false;
    }
    for(int i = 1; i < cards.size(); i++){
        if(num[i]!=num[i-1]+1)
            return false;
    }
    return true;
}
bool isFlash(vector<string> &cards){
    char suit=cards[0][1];
    for(int i = 1; i < cards.size(); i++){
        if(suit!=cards[i][1])
            return false;
    }
    return true;
}
bool isFullHouse(vector<string> &cards){
    if(isOnePair(cards)&&isThreeCard(cards))
        return true;
    return false;
}
bool isFourCard(vector<string> &cards){
    int tmp[100];
    fill(tmp,tmp+100,0);
    for(int i = 0; i < cards.size(); i++)
        tmp[markToNum[cards[i][0]]]++;
    int cnt=0;
    for(int i = 1; i <= 13; i++){
        if(tmp[i]>=4)
            return true;
    }
    return false;
}
bool isStraighFlash(vector<string> &cards){
    if(isFlash(cards)&&isStraight(cards))
        return true;
    return false;
}
bool isRoyalStraightFlash(vector<string> &cards){
    if(isStraighFlash(cards)){
        bool k=false;
        bool o=false;
        for(int i = 0; i < cards.size(); i++){
            if(cards[i][0]=='K')
                k=true;
            else if(cards[i][0]=='A')
                o=true;
        }
        if(k&&o)
            return true;
    }
    return false;
}

// ツ偲ィツつゥツづァツスツコツアツづーツ計ツ算
int calcScore(vector<string> &cards){
    int base=0;
    for(int i = 0; i < cards.size(); i++)
        base+=score[cards[i][1]][markToNum[cards[i][0]]];
    int p=0;
    if(isRoyalStraightFlash(cards))
        p=per[8];
    else if(isStraighFlash(cards))
        p=per[7];
    else if(isFourCard(cards))
        p=per[6];
    else if(isFullHouse(cards))
        p=per[5];
    else if(isFlash(cards))
        p=per[4];
    else if(isStraight(cards))
        p=per[3];
    else if(isThreeCard(cards))
        p=per[2];
    else if(isTwoPair(cards))
        p=per[1];
    else if(isOnePair(cards))
        p=per[0];
    return p*base;
}

int main(){
    
    for(int i = 1; i <= 13; i++){
        if(i==1)
            markToNum['A']=1;
        else if(i<=9)
            markToNum[i+'0']=i;
        else if(i==10)
            markToNum['T']=10;
        else if(i==11)
            markToNum['J']=11;
        else if(i==12)
            markToNum['Q']=12;
        else if(i==13)
            markToNum['K']=13;
    }

    int n;
    bool f=false;
    while(cin>>n){
        if(f)
            cout<<endl;
        f=true;
        for(int i = 1; i <= 13; i++)cin>>score['S'][i];
        for(int i = 1; i <= 13; i++)cin>>score['C'][i];
        for(int i = 1; i <= 13; i++)cin>>score['H'][i];
        for(int i = 1; i <= 13; i++)cin>>score['D'][i];
        for(int i = 0; i < 9; i++)cin>>per[i];
        for(int i = 0; i < n; i++){
            vector<string> cards;
            for(int j = 0; j < 5; j++){
                string s;
                cin>>s;
                cards.push_back(s);
            }
            cout<<calcScore(cards)<<endl;
        }
    }

    return 0;
}

Measured memory_bytes: 0