#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<cmath>
#include<algorithm>
using namespace std;
typedef vector<int> vi;

int main()
{
  int buckets[14];
  int buckets2[5];
  bool firsttime=true;
  int n;
  while(cin >> n)
  {
    if(!n) break;
    if(!firsttime) cout << endl;
    if(firsttime) firsttime=false;
    int cardscore[14][5];
    int handscore[9];
      for(int j=1;j<=4;j++)
      {
        for(int k=1;k<=13;k++)
        {
          cin >> cardscore[k][j];
        }
      }
      for(int j=0;j<9;j++)
      {
        cin >> handscore[j];
      }
    
    for(int i=0;i<n;i++)
    {
      int times=0;
      long long score=0;
      vector<string> card(5);
      vector<pair<int,int> > tmpc(5);
      for(int j=0;j<5;j++)
      {
        cin >> card[j];
        if(card[j][0]=='A')
        {
          tmpc[j].first=1;
        }
        else if(card[j][0]=='T')
        {
          tmpc[j].first=10;
        }
        else if(card[j][0]=='J')
        {
          tmpc[j].first=11;
        }
        else if(card[j][0]=='Q')
        {
          tmpc[j].first=12;
        }
        else if(card[j][0]=='K')
        {
          tmpc[j].first=13;
        }
        else
        {
          tmpc[j].first=(int)(card[j][0]-'0');
        }
        if(card[j][1]=='S')
        {
          tmpc[j].second=1;
        }
        else if(card[j][1]=='C')
        {
          tmpc[j].second=2;
        }
        else if(card[j][1]=='H')
        {
          tmpc[j].second=3;
        }
        else if(card[j][1]=='D')
        {
          tmpc[j].second=4;
        }
        score+=cardscore[tmpc[j].first][tmpc[j].second];

      }
      int onep=0,threec=0,four=0;
      bool flash=false;
      bool straight=true;
      sort(tmpc.begin(),tmpc.end());
      memset(buckets,0,sizeof(buckets));
      memset(buckets2,0,sizeof(buckets2));
      for(int j=0;j<5;j++)
      {
        buckets[tmpc[j].first]++;
        buckets2[tmpc[j].second]++;
      }
      for(int j=1;j<=13;j++)
      {
        if(buckets[j]==2)
        {
          onep++;
        }
        else if(buckets[j]==3)
        {
          threec++;
        }
        else if(buckets[j]==4)
        {
          four++;
        }
      }
      for(int j=1;j<=4;j++)
      {
        if(buckets2[j]==5)
        {
          flash=true;
          times=handscore[4];
        }
      }
      if(onep==1 && threec==0)
      {
        times=handscore[0];
      }
      else if(onep==2)
      {
        times=handscore[1];
      }
      else if(threec==1 && onep==0)
      {
        times=handscore[2];
      }
      else if(threec==1 && onep==1)
      {
        times=handscore[5];
      }
      else if(four==1)
      {
        times=handscore[6];
      }
      if(tmpc[0].first==1 && tmpc[1].first==10 && tmpc[2].first==11 && tmpc[3].first==12 && tmpc[4].first==13)
      {
        if(flash) times=handscore[8];
        else times=handscore[3];
      }
      else
      {
        for(int j=0;j<4;j++)
        {
          if(tmpc[j+1].first!=(tmpc[j].first+1)) straight=false;
        }
        if(straight)
        {
          if(flash) times=handscore[7];
          else times=handscore[3];
        }
      }
      cout << score*times << endl;
    }
  }
  return 0;
}


Measured memory_bytes: 3156