#include <iostream>
#include <string>
#include <limits>

using namespace std;

/* Total number of columns in board */
#define COLS 5

/* Total number of rows in board */
#define ROWS 5

/* Maximum number of input patterns */
#define PATTERNS 19

/*
 * Store each input pattern in the form of a bit vector. Each bit is 1 if
 * that square in the input pattern contains an 'X'. The bit vector is stored
 * in row major order.
 */
int pattern[PATTERNS];

/* How many numbers have already been called for each letter */
int called[COLS];

/* Total number of input patterns for each round */
int pattern_num;

/* The minimum number of patterns that must be combined for solution */
int comb_num;

/* How many numbers that still must be called to complete a solution */
int result;

/* Helper macro for computing bit position in the bit vector */ 
#define MASK(row, col) (1 << (((row) * COLS) + (col)))

/* Helper macro for testing/setting/clearing bit vector values */
#define TST(vector, row, col) ((vector) & MASK((row), (col)))
#define SET(vector, row, col) ((vector) |= MASK((row), (col)))

/* Initialize board layout from the input */
void parse_patterns(void)
{
    int row, col, pattern_idx;
    
    /* Clear out patterns from previous input */
    for(pattern_idx = 0; pattern_idx < PATTERNS; pattern_idx++)
        pattern[pattern_idx] = 0;

    /* Iterate over all input lines, each of which has a pattern row */
    for(row = 0; row < ROWS; row++) {
        for(pattern_idx = 0; pattern_idx < pattern_num; pattern_idx++) {
            string text;
        
            /* Read in one row of a pattern at a time */
            cin >> text;
        
            /* Verify the string contains all the columns */
            if(text.size() < COLS)
                throw "Invalid input pattern";

            /* Process all the columns */
            for(col = 0; col < COLS; col++) {
                char c = text[col];
            
                /* Read & verify the next character */
                if(c != 'O' && c != 'X')
                    throw "Invalid character in input pattern";

                /* Count all the 'X's found in input patterns */
                if(c == 'X')
                    SET(pattern[pattern_idx], row, col);
            }
        }       
    }    
}

/* Print out a given pattern */
void print_pattern(int pattern)
{
    int row, col;
    
    for(row = 0; row < ROWS; row++) {
        for(col = 0; col < COLS; col++) {
            
            if(TST(pattern, row, col))
                cout << "X";
            else
                cout << "O";
        }
        cout << endl;
    }
    
    cout << endl;
}

/* Generate all unique combinations of input patterns */
void combine(int start, int num, int comb)
{
    int i;
    
    /* Keep combining patterns until a complete pattern */
    if(num < comb_num) {    
        for(i = start; i <= num + (pattern_num - comb_num); i++)
            combine(i + 1, num + 1, comb | pattern[i]);
    }
    
    /* Check completed combination against called numbers */
    else {
        int actual[COLS] = { 0, 0, 0, 0, 0};
        int row, col, count = 0;
        
        /* Count the number of letters in each column in combination */
        for(row = 0; row < ROWS; row++)
            for(col = 0; col < COLS; col++)
                if(TST(comb, row, col))
                    actual[col]++;

        /* The center location is a freebie so don't include it in the count */
        if(TST(comb, ROWS/2, COLS/2))
            actual[COLS/2]--;
                             
        /* For each column, compare called numbers to numbers still needed */
        for(col = 0; col < COLS; col++)
            if(actual[col] > called[col])
                count += actual[col] - called[col];
                
        /* If this solution is more optimal, update global result */
        if(count < result)
            result = count;                
    }
}

/* Main body of program */
void process(void)
{
    int game_num, game_idx;
    
    /* Throw exceptions on unexpected EOF */
    cin.exceptions(ios::eofbit);
       
    /* Read in the number of inputs */
    cin >> game_num;

    /* Process each game separately */
    for(game_idx = 0; game_idx < game_num; game_idx++) {
        int i;

        /* Read how many numbers have been called already */
        for(i = 0; i < COLS; i++)
            cin >> called[i];
            
        /* Read number of input patterns and combinations */
        cin >> pattern_num >> comb_num;

        /* Initialize the final result */
        result = numeric_limits<int>::max();

        /* Initialize input patterns */
        parse_patterns();
        
        /* Generate all combinations and test against called numbers */
        combine(0, 0, 0);
        
        /* Print out the final result */
        cout << result << endl;
    }
}

/* Run program and print out any exceptions that occur */
int main(void)
{
    /* Run main body of code */
    try {
        process();
    }
    
    /* Catch any internally generated exceptions */
    catch(char const *e) {
        cerr << "Exception: " << e << endl;
    }
    
    /* Catch unexpected EOF on input */
    catch(ios::failure const &ee) {
        cerr << "Exception: Unexpected EOF on input" << endl;
    }
    
    return 0;
}