#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int same(vector<int> &c){
	for(int i = 1 ; i < c.size() ; i++)
		if(c[i] != c[i-1]) return false;
	return true;
}

int royal(vector<int> &v,vector<int> &c){
	return same(c) && v[0] == 1 && v[1] == 10 && v[2] == 11 && v[3] == 12 && v[4] == 13;
}
int straightflash(vector<int> &v,vector<int> &c){
	if(!same(c))return false;
	if(v[0] == 1 && v[1] == 10 && v[2] == 11 && v[3] == 12 && v[4] == 13) return true;
	for(int i = 1; i < v.size() ; i++)
		if(v[i] != v[i-1]+1) return false;
	return true;
}
int straight(vector<int> &v,vector<int> &c){
	if(v[0] == 1 && v[1] == 10 && v[2] == 11 && v[3] == 12 && v[4] == 13) return true;
	for(int i = 1; i < v.size() ; i++){
		if(v[i] != v[i-1]+1) return false;
	}
	return true;
}
int four(vector<int> &v,vector<int> &c){
	for(int i = 0 ; i < v.size() ; i++){
		if(count(v.begin(),v.end(),v[i]) == 4) return true;
	}
	return false;
}
int fullhouse(vector<int> &v,vector<int> &c){
	bool f1 = 0 , f2 = 0;
	for(int i = 0 ; i < v.size() ; i++){
		if(count(v.begin(),v.end(),v[i]) == 3) f1 = true;
		if(count(v.begin(),v.end(),v[i]) == 2) f2 = true;
	}
	return f1 & f2;
}

int three(vector<int> &v,vector<int> &c){
	for(int i = 0 ; i < v.size() ; i++){
		if(count(v.begin(),v.end(),v[i]) == 3) return true;
	}
	return false;
}

int two(vector<int> &v,vector<int> &c){
	int k[10] = {0};
	for(int i = 0 ; i < v.size() ; i++){
		k[count(v.begin(),v.end(),v[i])]++;
	}
	return k[2] == 4;
}

int one(vector<int> &v,vector<int> &c){
	int k[10] = {0};
	for(int i = 0 ; i < v.size() ; i++){
		k[count(v.begin(),v.end(),v[i])]++;
	}
	return k[2] == 2;
}

int flash(vector<int> &v,vector<int> &c){
	return same(c);
}
int num(char c){
	if(c == 'A') return 1;
	else if(c == 'T') return 10;
	else if(c == 'J') return 11;
	else if(c == 'Q') return 12;
	else if(c == 'K') return 13;
	else return c - '0';
}
int suit(char c){
	switch(c){
	case 'S':return 1;
	case 'C':return 2;
	case 'H':return 3;
	case 'D':return 4;
	};
}
int main(){
	int n;
	int (*p[9]) (vector<int> &v,vector<int> &c);
	p[0] = one;
	p[1] = two;
	p[2] = three;
	p[3] = straight;
	p[4] = flash;
	p[5] = fullhouse;
	p[6] = four;
	p[7] = straightflash;
	p[8] = royal;
	int T = 0;
	while(cin >> n){
		if(T++)cout << endl;
		int w[5][14];
		for(int y = 1 ; y <= 4 ; y++){
			for(int i = 1 ; i <= 13 ; i++){
				cin >> w[y][i];
			}		
		}
		
		int rate[9];
		for(int i = 0 ; i < 9 ; i++){
			cin >> rate[i];
		}
		for(int _ = 0 ; _ < n ; _++){
			int o = 0;
			vector<int> v; vector<int> c;
			for(int i = 0 ; i < 5 ; i++){
				string s;
				cin >> s;
				v.push_back(num(s[0]));
				c.push_back(suit(s[1]));
				o += w[suit(s[1])][num(s[0])];
			}
			for(int __ = 0 ; __ < 5 ; __++){
				for(int i = 0 ; i < 4 ; i++){
					if(v[i] > v[i+1]){
						swap(v[i],v[i+1]);
						swap(c[i],c[i+1]);
					}
				}
			}
			int ans = 0;
			for(int i = 0 ; i < 9 ; i++){
				ans = max( p[i](v,c) * o * rate[i] , ans);
			}
			cout << ans << endl;
		}
		
	}

}