#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;

#define REP(i,n) for(int i=0;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()
const int INF = 1<<29;

enum SUIT { C, D, H, S };
struct card {
  int suit, rank;
  card(const string& s) {
    rank = string("0A23456789TJQK").find(s[0]);
    suit = string("SCHD").find(s[1]);
  }
  bool operator<(const card& o) const {
    return rank != o.rank ? rank < o.rank : suit < o.suit;
  }
};
enum {
  Highest_Card, One_Pair, Two_Pairs, Three_of_a_Kind,
  Straight, Flush, Full_House, Four_of_a_Kind, Straight_Flush, Royal_Straight_Flush };
int poker_hand(const vector<card>& hand) {
  vector<int> X(27), Y(4);
  for (int i = 0; i < hand.size(); ++i) {
    ++X[hand[i].rank]; ++Y[hand[i].suit];
    ++X[hand[i].rank+13]; // for wrapping
  }
  int I = 1, J = 2, K = 0;
  if (X[I] < X[J]) swap(I, J);
  for (int i = 3; i < 14; ++i) {
    if (X[i] >= X[I]) J = I, I = i;
    else if (X[i] >= X[J]) J = i;
  }
  for (int k = 0; k < 4; ++k)
    if (Y[k] >= Y[K]) K = k;
  bool straight = false;
  for (int i = 1; i+4 <= 14; ++i) // wrapping range
    if (X[i+0] && X[i+1] && X[i+2] && X[i+3] && X[i+4]) straight = true;
  bool flush = (Y[K] == 5);
  // REP(i, 13)
  //   cout << X[i+1] << " ";
  // cout << endl;
  if (straight && flush && X[13] && X[1]) return Royal_Straight_Flush;
  if (straight && flush) return Straight_Flush;
  if (X[I] == 4) return Four_of_a_Kind;
  if (X[I] == 3 && X[J] == 2) return Full_House;
  if (flush) return Flush;
  if (straight) return Straight;
  if (X[I] == 3) return Three_of_a_Kind;
  if (X[I] == 2 && X[J] == 2) return Two_Pairs;
  if (X[I] == 2 && X[J] == 1) return One_Pair;
  return Highest_Card;
}

int main() {
  int n;
  bool f = 0;
  while(cin>>n) {
    if (f) cout << endl;
    f = 1;
    int ten[4][13];
    REP(i,4) {
      REP(j,13)
        cin >> ten[i][j];
    }
    int bai[10];
    bai[0] = 0;
    REP(i,9)
      cin >> bai[i+1];
    REP(i,n) {
      vector<card> v;
      REP(j,5) {
        string s;
        cin >> s;
        v.push_back(card(s));
      }
      int score = 0;
      FOR(it, v) {
        score += ten[it->suit][it->rank-1];
        // cout << "("<<it->suit << " " << it->rank-1<<")";
        // cout << score << " ";
      }
      //    cout << endl;
      cout << score * bai[poker_hand(v)] << endl;
      
    }
  }
}