// Arup Guha
// 5/11/2020
// Solution to COP 3502 Week #1 Program using no dynamic memory allocation.

#include <stdio.h>
#include <string.h>

// Limits on the dictionary of words.
const int MAXWORDS = 100;
const int MAXSIZE = 7;

// Number of letters...
const int NUMLETTERS = 26;

// Scores of all tiles in Scrabble.
const int TILESCORES[] = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};

// Function Prototypes
int canForm(int wordFreq[], int tileFreq[]);
void fillFreq(char* word, int freq[]);
int score(char* word,int letterMult[], int wordMult[]);

int main(void) {

    int numCases;
    scanf("%d", &numCases);

    // Process each case.
    for (int loop=0; loop<numCases; loop++) {

        int numWords;
        scanf("%d", &numWords);

        // Read in words.
        char words[MAXWORDS][MAXSIZE+1];
        for (int i=0; i<numWords; i++)
            scanf("%s", words[i]);

        // Read in your tiles.
        char tiles[MAXSIZE+1];
        scanf("%s", tiles);

        // Store all multipliers here.
        int letterMult[MAXSIZE];
        int wordMult[MAXSIZE];

        // Read in letter multipliers.
        for (int i=0; i<MAXSIZE; i++)
            scanf("%d", &letterMult[i]);

        // Read in word multipliers.
        for (int i=0; i<MAXSIZE; i++)
            scanf("%d", &wordMult[i]);

        // Initialize tile frequency.
        int tileFreq[NUMLETTERS];
        for (int i=0; i<26; i++)
            tileFreq[i] = 0;

        // Calculate letter frequencies of tiles.
        fillFreq(tiles, tileFreq);

        // Store the answer here.
        int best = 0;

        // Try each word.
        for (int i=0; i<numWords; i++) {

            // Get the letter frequencies of this word.
            int wordFreq[NUMLETTERS];
            for (int j=0; j<NUMLETTERS; j++)
                wordFreq[j] = 0;
            fillFreq(words[i], wordFreq);

            // Skip words we can't form.
            if (!canForm(wordFreq, tileFreq)) continue;

            // Score this word.
            int curScore = score(words[i],letterMult, wordMult);

            // Update if this word is best one yet.
            if (curScore > best) best = curScore;
        }

        // Output the best possible score.
        printf("%d\n", best);

    }

    return 0;
}

// Pre-condition: wordFreq stores the frequency of letters in a potential
//                word, tileFreq stores the frequency of letters in a set
//                of tiles.
// Post-condition: Returns 1 if the tiles can be used to form the word and
//                 0 otherwise.
int canForm(int wordFreq[], int tileFreq[]) {

    // If we ever need more of a letter than we have, we can't do it.
    for (int i=0; i<NUMLETTERS; i++)
        if (wordFreq[i] > tileFreq[i])
            return 0;

    // If we get here we are good.
    return 1;
}

// Pre-condition: freq is size 26 and stores all 0s, word is uppercase
//                letters only.
// Post-condition: freq is filled with the frequencies of the letters
//                 in word. freq[0] stores # of As, freq[1] stores # of Bs, etc.
void fillFreq(char* word, int freq[]) {

    // To save time just do this once.
    int len = strlen(word);

    // Mark each letter.
    for (int i=0; i<len; i++)
        freq[word[i]-'A']++;
}

// Pre-condition: word is 7 or fewer upper case letters, letterMult and
//                wordMult are of size 26, all values in letterMult are 1
//                except for at most 1, and all values in both arrays are
//                1, 2 or 3.
// Post-condition: The score of word according to the simplified Scrabble
//                 rules given is returned.
int score(char* word,int letterMult[], int wordMult[]) {

    int total = 0, len = strlen(word), mult = 1;

    // Go through each letter and add up letter scores with letter multipliers.
    // Also, multiply together the word multiplier.
    for (int i=0; i<len; i++) {
        total += (TILESCORES[word[i]-'A']*letterMult[i]);
        mult *= wordMult[i];
    }

    // This is the final score of the word.
    return mult*total;
}
