card_table = {'A': 0, '2': 1, '3': 2, '4': 3, '5': 4, '6': 5, '7': 6, '8': 7, '9': 8, 'T': 9, 'J': 10, 'Q': 11, 'K': 12}
suit_table = {'S': 0, 'C': 1, 'H': 2, 'D': 3}


def get_numlist(cards):
    numlist = []
    for card in cards:
        numlist.append(card[0])
    return numlist


def is_royal(cards):
    numlist = get_numlist(cards)
    if set(numlist) == set(['T', 'J', 'Q', 'K', 'A']):
        return True
    else:
        return False


def is_fourcard(cards):
    numlist = get_numlist(cards)
    if numlist.count(numlist[0]) == 4 or numlist.count(numlist[1]) == 4:
        return True
    else:
        return False


def is_fullhouse(cards):
    numlist = get_numlist(cards)
    if len(set(numlist)) == 2:
        for num in numlist:
            if numlist.count(num) == 3:
                return True
    return False


def is_flash(cards):
    suitlist = []
    for card in cards:
        suitlist.append(card[1])
    if len(set(suitlist)) == 1:
        return True
    else:
        return False


def is_straight(cards):
    numlist = get_numlist(cards)
    if len(set(numlist)) == 5:
        index_list = []
        for num in numlist:
            index_list.append(card_table[num])
        index_list.sort()
        if index_list == range(index_list[0], index_list[4]+1) or index_list == [0, 9, 10, 11, 12]:
            return True
    return False


def is_threecard(cards):
    numlist = get_numlist(cards)
    for num in numlist:
        if numlist.count(num) == 3:
            return True
    return False


def is_twopair(cards):
    count = 0
    numlist = get_numlist(cards)
    for num in set(numlist):
        if numlist.count(num) == 2:
            count += 1
        if count == 2:
            return True
    return False


def is_onepair(cards):
    numlist = get_numlist(cards)
    for num in numlist:
        if numlist.count(num) == 2:
            return True
    return False


def get_hand(cards, rate):
    if is_flash(cards) and is_straight(cards):
        if is_royal(cards):
            return rate[8]
        else:
            return rate[7]
    if is_fourcard(cards):
        return rate[6]
    if is_fullhouse(cards):
        return rate[5]
    if is_flash(cards):
        return rate[4]
    if is_straight(cards):
        return rate[3]
    if is_threecard(cards):
        return rate[2]
    if is_twopair(cards):
        return rate[1]
    if is_onepair(cards):
        return rate[0]
    return 0


def sum_bps(cards, bps):
    bp = 0
    for card in cards:
        num = card_table[card[0]]
        suit = suit_table[card[1]]
        bp += bps[suit][num]
    return bp

first_flag = False
while(1):
    try:
        input_num = int(raw_input())
        if first_flag:
            print
        bps = []
        for count in range(4):
            i = raw_input()
            bps.append(map(int, i.split()))
        i = raw_input()
        rate = map(int, i.split())
        for count in range(input_num):
            i = raw_input()
            cards = i.split()
            hand_rate = get_hand(cards, rate)
            bp = sum_bps(cards, bps)
            print hand_rate * bp
        first_flag = True
    except:
        break

Measured memory_bytes: 4472