#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>

#define REP(i,n) for(int i=0;i<n;++i)
#define REPR(i,n) for(int i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

//suit S:0 C:1 H:2
int conNum(char c){
  if(c == 'K') return 13;
  else if(c == 'Q') return 12;
  else if(c == 'J') return 11;
  else if(c == 'T') return 10;
  else if(c == 'A') return 1;
  else{
    return c - '0';
  }
}

int suitTonum(char c){
  if(c == 'S') return 0;
  else if(c == 'C') return 1;
  else if(c == 'H') return 2;
  else return 3;
}

struct hand{
  int num[5];
  int suit[5];
};

int main(){

  int n;
  bool hoge = false;
  while(cin >> n){
    if(hoge) cout << "\n";
    hoge = true;
    llong card[4][13] = {};
    REP(i,4){
      REP(j,13){
        cin >> card[i][j];
      }
    }
    llong role[9];
    REP(i,9){
      cin >> role[8 - i];
    }

    vector<hand> v(n);
    REP(i,n){
      REP(j,5){
        string tmp;
        cin >> tmp;
        v[i].num[j] = conNum(tmp[0]) - 1;
        v[i].suit[j] = suitTonum(tmp[1]);

        //cout << "#i" << i << " " << v[i].num[i] << " " << v[i].suit[i] << "\n";
      }
    }

    vector<llong> base(n);
    REP(i,n){
      base[i] = 0;
      REP(j,5){
        int a = v[i].num[j], b = v[i].suit[j];
        base[i] += card[b][a];
      }
      sort(v[i].num, v[i].num + 5);
    }
    REP(i,n){

      //flush
      bool end = false;
      bool isFl = true;
      REP(j,4){
        if(v[i].suit[j+1] != v[i].suit[0]){
          isFl = false;
          break;
        }
      }
      //st
      bool isSt = true;
      int last = v[i].num[0];
      REP(j,4){
        if(v[i].num[j+1] != last + 1){
          isSt = false;
          break;
        }
        last = v[i].num[j+1];
      }
      
      bool isRoyal = false;
      if(v[i].num[0] == 0 && v[i].num[1] == 9 && v[i].num[2] == 10 && v[i].num[3] == 11 && v[i].num[4] == 12){
        isSt = true;
        if(isFl){
          cout << base[i] * role[0] << "\n";
          isRoyal = true;
        }
      }
      if(isRoyal) continue;
      if(isFl && isSt){
        cout << base[i] * role[1] << "\n";
        continue;
      }

      map<int, int> mp;
      REP(j,5){
        mp[v[i].num[j]]++;
      }
      //four
      REPI(itr,mp){
        if(itr->second == 4){
          cout << base[i] * role[2] << "\n";
          end = true;
          break;
        }
      }
      if(end) continue;
      
      //full
      int th = 0, tw = 0;
      REPI(itr,mp){
        if(itr->second == 3){
          th++;
        }else if(itr->second == 2){
          tw++;
        }
      }
      if(th > 0 && tw){
        cout << base[i] * role[3] << "\n";
        continue;
      }

      if(isFl){
        cout << base[i] * role[4] << "\n";
        continue;
      }
      if(isSt){
        cout << base[i] * role[5] << "\n";
        continue;
      }
      if(th > 0){
        cout << base[i] * role[6] << "\n";
        continue;
      }
      if(tw == 2){
        cout << base[i] * role[7] << "\n";
        continue;
      }
      if(tw == 1){
        cout << base[i] * role[8] << "\n";
        continue;
      }
      cout << "0\n";
    }
  }
  return 0;
}




Measured memory_bytes: 3160