#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

enum Hand {
  BUTA,
  ONE_PAIR,
  TWO_PAIR,
  THREE_CARD,
  STRAIGHT,
  FLUSH,
  FULL_HOUSE,
  FOUR_CARD,
  STRAIGHT_FLUSH,
  ROYAL_STRAIGHT_FLUSH
};

int mapto[200];
int hand[10];
int point[52];
int card[5];
int exist_number[13];
int exist_suit[4];

Hand handCheck() {
  bool royal = false;
  bool straight = false;;
  bool flush = false;
  bool exist_four = false;
  bool exist_three = false;
  int  exist_pair = 0;
  MEMSET(exist_number, false);
  MEMSET(exist_suit, false);
  REP(i, 5) {
    int number = card[i] % 13;
    int suit = card[i] / 13;
    exist_number[number]++;
    exist_suit[suit]++;
  }
  if (exist_number[0] && exist_number[9] && exist_number[10] && exist_number[11] && exist_number[12]) {
    royal = true;
    straight = true;
  }
  REP(i, 9) {
    REP(j, 5) {
      assert(i + j < 13);
      if (!exist_number[i + j]) { goto next; }
    }
    straight = true;
next:;
  }
  REP(i, 4) {
    if (exist_suit[i] == 5) { flush = true; }
  }
  REP(i, 13) {
    if (exist_number[i] == 4) { exist_four = true; }
    else if (exist_number[i] == 3) { exist_three = true; }
    else if (exist_number[i] == 2) { exist_pair++; }
  }
  if (royal && flush) { return ROYAL_STRAIGHT_FLUSH; }
  if (straight && flush) { return STRAIGHT_FLUSH; }
  if (exist_four) { return FOUR_CARD; }
  if (exist_three && exist_pair) { return FULL_HOUSE; }
  if (flush) { return FLUSH; }
  if (straight) { return STRAIGHT; }
  if (exist_three) { return THREE_CARD; }
  if (exist_pair == 2) { return TWO_PAIR; }
  if (exist_pair == 1) { return ONE_PAIR; }
  return BUTA;
}

int main() {
  MEMSET(mapto, -1);
  mapto['A'] = 0;
  mapto['2'] = 1;
  mapto['3'] = 2;
  mapto['4'] = 3;
  mapto['5'] = 4;
  mapto['6'] = 5;
  mapto['7'] = 6;
  mapto['8'] = 7;
  mapto['9'] = 8;
  mapto['T'] = 9;
  mapto['J'] = 10;
  mapto['Q'] = 11;
  mapto['K'] = 12;
  mapto['S'] = 0;
  mapto['C'] = 1;
  mapto['H'] = 2;
  mapto['D'] = 3;
  hand[BUTA] = 0;
  bool first = true;
  int n;
  while (scanf("%d", &n) != EOF) {
    assert(1 <= n && n <= 10000);
    REP(suit, 4) {
      REP(i, 13) {
        scanf("%d", &point[i + suit * 13]);
        assert(0 <= point[i] && point[i] <= 10000);
      }
    }
    if (!first) { puts(""); }
    first = false;
    REP(i, 9) {
      scanf("%d", &hand[i + 1]);
      assert(0 <= hand[i + 1] && hand[i + 1] <= 10000);
      assert(hand[i] <= hand[i + 1]);
    }
    REP(iter, n) {
      int base = 0;
      REP(i, 5) {
        char c1, c2;
        scanf(" %c %c ", &c1, &c2);
        int number = mapto[(int)c1];
        int suit = mapto[(int)c2];
        assert(0 <= number && number < 13);
        assert(c1 != 'S' && c1 != 'C' && c1 != 'H' && c1 != 'D');
        assert(0 <= suit && suit < 4);
        assert(c2 != 'A' && c2 != '2' && c2 != '3' && c2 != '4');
        card[i] = number + suit * 13;
        base += point[card[i]];
        REP(j, i) {
          assert(card[i] != card[j]);
        }
      }
      sort(card, card + 5);
      printf("%d\n", base * hand[handCheck()]);
    }
  }
}

Measured memory_bytes: 896