x = int(input()) def function(databaseword): words = [] word = '' for index in range(len(databaseword)): if databaseword[index] == ' ': words += [word] word = '' else: word += str(databaseword[index]) words += [word] word = '' length = len(words) for index in range(length): word = words[index] for index1 in range(index+1, length): word += ' ' word += words[index1] words.append(word) return words #"Enter the number of database and number of queries (separate by a space): " for numberofdatabase in range(1,x+1): print( 'Case ' + str(numberofdatabase) + ':' ) y, z = input().split() # Y is the database y = int(y) # Z is the query z = int(z) # The DataBase database = [] for i in range(y): # "Enter Database " + str(i+1) + ":" database.append(function(input())) # The Queries query=[] for i in range(z): query.append(input()) for q in query: count = 0 for d in database: if q in d: count+=1 print(count)