using System.Collections.Generic;
using System.Linq;
using System;

public class P
{
    public char s { get; set; }
    public int n { get; set; }
}

public class hello
{
    public static void Main()
    {
        var c = "0023456789TJQKA";
        var d = new Dictionary<char, int>();
        for (int i = 2; i <= 14; i++) d[c[i]] = i;
        var d2 = new Dictionary<char, int>() { { 'S', 0 }, { 'C', 1 }, { 'H', 2 }, { 'D', 3 } };
        var first = true;
        string s;
        for (; (s = Console.ReadLine()) != null;)
        {
            if (first) { first = false; }
            else Console.WriteLine();
            var n = int.Parse(s.Trim());
            var bp = new int[4, 13];
            for (int i = 0; i < 4; i++)
            {
                string[] line = Console.ReadLine().Trim().Split(' ');
                for (int j = 0; j < 13; j++)
                    bp[i, j] = int.Parse(line[j]);
            }
            string[] line2 = Console.ReadLine().Trim().Split(' ');
            var handp = Array.ConvertAll(line2, int.Parse);
            var p = 0;
            while (p++ < n)
            {

                var ps = new P[5];
                line2 = Console.ReadLine().Trim().Split(' ');
                var bpsum = 0;
                for (int i = 0; i < 5; i++)
                {
                    var suit = line2[i][1];
                    var wn = d[line2[i][0]];
                    ps[i] = new P { s = suit, n = wn };
                    int wp = wn == 14 ? 0 : wn - 1;
                    bpsum += bp[d2[suit], wp];
                }
                if (bpsum == 0) { Console.WriteLine(0); continue; }
                var whp = checkHand(ps);
                Console.WriteLine(whp == 0? 0: bpsum * handp[whp - 1]);
            }
        }
    }
    static int checkHand(P[] ps)
    {
        if (royalstraigthflush(ps)) return 9;
        if (straight(ps) && flush(ps)) return 8;
        if (card4(ps)) return 7;
        if (fullhouse(ps)) return 6;
        if (flush(ps)) return 5;
        if (straight(ps)) return 4;
        if (card3(ps)) return 3;
        if (pair2(ps)) return 2;
        if (pair1(ps)) return 1;
        return 0;
    }
    static bool pair1(P[] ps)
    {
        var d = new Dictionary<int, int>();
        foreach (var x in ps)
        {
            if (d.ContainsKey(x.n)) d[x.n]++;
            else d[x.n] = 1;
        }
        return d.Count() == 4;
    }
    static bool pair2(P[] ps)
    {
        var d = new Dictionary<int, int>();
        foreach (var x in ps)
        {
            if (d.ContainsKey(x.n)) d[x.n]++;
            else d[x.n] = 1;
        }
        return d.Count() == 3;
    }
    static bool card3(P[] ps)
    {
        var d = new Dictionary<int, int>();
        foreach (var x in ps)
        {
            if (d.ContainsKey(x.n)) d[x.n]++;
            else d[x.n] = 1;
        }
        foreach (var x in d)
        {
            if (x.Value == 3) return true;
        }
        return false;
    }
    static bool fullhouse(P[] ps)
    {
        var d = new Dictionary<int, int>();
        foreach (var x in ps)
        {
            if (d.ContainsKey(x.n)) d[x.n]++;
            else d[x.n] = 1;
        }
        return d.Count() == 2;
    }
    static bool card4(P[] ps)
    {
        var d = new Dictionary<int, int>();
        foreach (var x in ps)
        {
            if (d.ContainsKey(x.n)) d[x.n]++;
            else d[x.n] = 1;
        }
        foreach (var x in d)
        {
            if (x.Value == 4) return true;
        }
        return false;
    }
    static bool royalstraigthflush(P[] ps)
    {
        var a = ps.OrderBy(x => x.n).ToArray();
        return (flush(a) && a[0].n == 10 && a[1].n == 11 && a[2].n == 12 && a[3].n == 13 && a[4].n == 14);
    }
    static bool straight(P[] ps)
    {
        var a = ps.OrderBy(x => x.n).ToArray();
        if (a[0].n == 2 && a[1].n == 3 && a[2].n == 4 && a[3].n == 5 && a[4].n == 14) return true;
        var t = a[0].n;
        for (int i = 1; i <= 4; i++)
        {
            if (a[i].n != t + 1) return false;
            t = a[i].n;
        }
        return true;
    }
    static bool flush(P[] a) => a[0].s == a[1].s && a[1].s == a[2].s && a[2].s == a[3].s && a[3].s == a[4].s;
}



Measured memory_bytes: 20508