#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int x[100][100];
int y[100][100];
int loop[100][100];

int check(int yy,int xx, int a){
  if(loop[yy][xx] == a) return 1;
  if(loop[yy][xx] == 0){
    loop[yy][xx] = a;
    return check(y[yy][xx],x[yy][xx],a);
  }
  return 0;
}

#define SPADE  0
#define CLOVER 1
#define HEART  2
#define DAIA   3

#define ONE_PAIR   1
#define TWO_PAIR   2
#define THREE_CARD 3
#define STRAIGHT   4
#define FLASH      5
#define FULL_HOUSE 6
#define FOUR_CARD  7
#define ST_FLASH   8
#define RYAL_ST_FL 9

int point[4][13];
int ypoint[10];

int main(){
  int n;
  int e[128];
  int m[128];
  int cc = 0;

  REP(i,10) m['0'+i] = i - 1;
  m['A'] =  0;
  m['T'] =  9;
  m['J'] = 10;
  m['Q'] = 11;
  m['K'] = 12;

  e['H'] = HEART;
  e['C'] = CLOVER;
  e['D'] = DAIA;
  e['S'] = SPADE;


  while(cin>>n){
    long long ans = 0;

    if(cc++ != 0) cout<<endl;
    REP(i,4) REP(j,13) cin>>point[i][j];
    REP(i,9) cin>>ypoint[i+1];

    while(n --> 0){
      pair<int,int> card[5];
      int base = 0;
      int yaku = 0;
      REP(i,5){
        char buff[3];
        cin>>buff;
        //printf("buff = %s\n",buff);
        card[i].f = m[buff[0]];
        card[i].s = e[buff[1]];

        base += point[card[i].s][card[i].f];

        if(card[i].f == 0) card[i].f = 13;
      }

      sort(card, card+5);

      int num[14] = {0};
      int nume[4] = {0};
      REP(i,5){
        num[card[i].f]++;
        nume[card[i].s]++;
      }

      int pcnt[6] = {0};
      REP(i,14) pcnt[num[i]]++;

      if(pcnt[2] == 1) yaku = ONE_PAIR;
      if(pcnt[2] == 2) yaku = TWO_PAIR;
      if(pcnt[3] == 1) yaku = THREE_CARD;

      bool straight = true;
      bool flash = false;
      int tcnt = 0;
      REP(i,4) if(card[i].f + 1 != card[i+1].f) straight = false;
      if(!straight){
        if(card[4].f == 13){
          straight = true;
          card[4].f = 0;
          sort(card, card+5);
          REP(i,4) if(card[i].f + 1 != card[i+1].f) straight = false;
          card[4].f = 13;
          sort(card, card+5);
        }
      }
      if(straight) yaku = STRAIGHT;

      REP(i,4) if(nume[i] == 5) flash = true;
      if(flash) yaku =FLASH;

      if(pcnt[2] == 1 && pcnt[3] == 1)
        yaku = FULL_HOUSE;

      if(pcnt[4] == 1)
        yaku = FOUR_CARD;

      if(flash && straight)
        yaku = ST_FLASH;

      if(flash && straight && card[0].f == 9)
        yaku = RYAL_ST_FL;

      //printf("base = %d, yaku = %d\n",base,yaku);
      ans = (ll)ypoint[yaku] * base;
      cout << ans << endl;
    }
  }

  return 0;
}

Measured memory_bytes: 884