def compareWordWithAvailableArray(histogram_letters):
    s = input()
    histogram_word = [0 for col in range(26)]
    for letter in s:
        histogram_word[ord(letter) - ord('a')] += 1
    for i in range(26):
        if histogram_word[i] > histogram_letters[i]:
            print("NIE")
            return
    print("TAK")

def test():
    Z = input().strip().split(' ')
    n = int(Z[0])
    k = int(Z[1])
    Z = input().strip().split(' ')
    histogram_letters = [0 for col in range(26)]
    for letter in Z:
        histogram_letters[ord(letter) - ord('A')] += 1
    for i in range(0,k):
        compareWordWithAvailableArray(histogram_letters)

T = int(input())
while T > 0:
    T -= 1
    test()
    print('')
