fun main(args:Array<String>):Unit {
    var isNotFirst = false
    while(true){
        val n = readLine()?.trim()?.toInt() ?: return
        if (isNotFirst) print("\n")
        isNotFirst = true
        val basePoint = Array(4){readLine()!!.trim().split(' ').map(String::toInt).toTypedArray()}
        val multiple = readLine()!!.trim().split(' ').map(String::toInt).toTypedArray()
        val score = ScoreBoard(basePoint, multiple)
        repeat(n){
            println(score.score(Cards(readLine()!!.trim().split(' ').map{Card(it)})))
        }
    }
}
class ScoreBoard(private val mBasePoints:Array<Array<Int>>, private val mMultiple:Array<Int>){
    private fun baseScore(card:Card):Int {
        return mBasePoints[when(card.suit){
            Suit.Spade -> 0
            Suit.Clover -> 1
            Suit.Hart -> 2
            Suit.Dia -> 3
        }][card.number - 1]
    }
    fun score(cards:Cards):Int {
        return cards.cards.sumBy{baseScore(it)} * when(cards.role()){
            Role.NoRole -> 0
            Role.OnePair -> mMultiple[0]
            Role.TwoPair -> mMultiple[1]
            Role.ThreeCards -> mMultiple[2]
            Role.Strait -> mMultiple[3]
            Role.Flush -> mMultiple[4]
            Role.FullHouse -> mMultiple[5]
            Role.FourCards -> mMultiple[6]
            Role.StraitFlush -> mMultiple[7]
            Role.RoyalStraitFlush -> mMultiple[8]
        }
    }
}
enum class Suit{
    Spade, Clover, Hart, Dia
}
class Card private constructor(val number:Int, val suit: Suit){
    constructor(str:String):this(when(str[0]){
        'A' -> 1
        'T' -> 10
        'J' -> 11
        'Q' -> 12
        'K' -> 13
        else -> str[0].toString().toInt()
    }, when(str[1]){
        'S' -> Suit.Spade
        'C' -> Suit.Clover
        'H' -> Suit.Hart
        else -> Suit.Dia
    })
}
enum class Role{
    NoRole, OnePair, TwoPair, ThreeCards, Strait, Flush, FullHouse, FourCards, StraitFlush, RoyalStraitFlush
}
class Cards(private val mCards:Iterable<Card>){
    val cards:Iterable<Card>
        get() = mCards
    fun role():Role {
        return when{
            isRoyalStraitFlush(mCards) -> Role.RoyalStraitFlush
            isStraitFlush(mCards) -> Role.StraitFlush
            isFourCards(mCards) -> Role.FourCards
            isFullHouse(mCards) -> Role.FullHouse
            isFlush(mCards) -> Role.Flush
            isStrait(mCards) -> Role.Strait
            isThreeCards(mCards) -> Role.ThreeCards
            isTwoPair(mCards) -> Role.TwoPair
            isOnePair(mCards) -> Role.OnePair
            else -> Role.NoRole
        }
    }
    companion object {
        private fun isRoyalStraitFlush(cards:Iterable<Card>):Boolean {
            return isFlush(cards) && isTenToAceStrait(cards)
        }
        private fun isFlush(cards:Iterable<Card>):Boolean {
            return Suit.values().any{suit -> cards.all{it.suit == suit}}
        }
        private fun isTenToAceStrait(cards:Iterable<Card>):Boolean {
            return listOf(1, 10, 11, 12, 13).all{num -> cards.any{it.number == num}}
        }
        private fun isStrait(cards:Iterable<Card>):Boolean {
            return (1 until 5).map{cards.minBy(Card::number)!!.number + it}.all{num -> cards.any{it.number == num}} || isTenToAceStrait(cards)
        }
        private fun isStraitFlush(cards:Iterable<Card>):Boolean {
             return isStrait(cards) && isFlush(cards)
        }
        private fun isFourCards(cards:Iterable<Card>):Boolean {
            return cards.groupBy(Card::number).any { it.value.size == 4 }
        }
        private fun isTwoPair(cards:Iterable<Card>):Boolean {
            return cards.groupBy(Card::number).filter{it.value.size == 2}.count() == 2
        }
        private fun isThreeCards(cards:Iterable<Card>):Boolean {
            return cards.groupBy(Card::number).any{it.value.size == 3}
        }
        private fun isOnePair(cards:Iterable<Card>):Boolean {
            return cards.groupBy(Card::number).any{it.value.size == 2}
        }
        private fun isFullHouse(cards:Iterable<Card>):Boolean {
            return isOnePair(cards) && isThreeCards(cards)
        }
    }
}


Measured memory_bytes: 125084